473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Any way to disable CSS via javascript?

I'm inserting elements into the DOM, but don't want them to inherit
*any* css at all (I can't control the CSS). Does anyone know if this
is possible?

I tried: element.style = null; But this doesn't work. I get an
error that "style" is read-only.

Am I going to have to loop over every possible style property and set
it to some "default" value?

eg:
//yes, i know, it's not proper JS. I forgot how to iterate over
objects.
defaults = {color: 'black', background: 'transparent', ... lots of
work ... };
foreach (defaults as key=>value){
eval("element.style." + key) = value;
}

That would suck.

Any ideas?

Thanks.
-sam
Jun 27 '08 #1
3 1236
On Jun 5, 1:34 pm, samm...@gmail.com wrote:
I'm inserting elements into the DOM, but don't want them to inherit
*any* css at all (I can't control the CSS). Does anyone know if this
is possible?
Something to bear in mind is that all elements have *some* kind of
styling applied. Section 6.4 of the CSS 2.1 specification (http://
www.w3.org/TR/CSS21/cascade.html#cascade) shows that Styles either
come from the Author, the User, or the User-Agent.

I don't make this point to be persnickety; rather, to suggest that the
way to approach this is to work out what you mean by *no* CSS and go
on from there.

I'm going to hazard a guess that by *no* CSS, you mean the kind of
styling that you would get in the absence of a stylesheet.

So, one option would be to add a new stylesheet like the sample one
provided in section 6.4 (mentioned above), but add a class selector to
each style. Something like 'defaultStyling' might do.

Then, with each new element that you create, set the class to
defaultStyling. You might hit some wrinkles with specificity, but this
an approach to consider.


[snip]
Jun 27 '08 #2
I'm going to hazard a guess that by *no* CSS, you mean the kind of
styling that you would get in the absence of a stylesheet.
Correct. Persnickety or not, good point. Let's just say I'd define
my own "defaults".

You suggest creating a stylesheet which defines all the "default"
styles that I'd be happy with for each classed element type
(defaultStyling div, defaultStyling h1, defaultStyling h2, ...many
more...). Problem is, within each element type's style I'd have to
find out which attributes could be inherited, and set them to the
"default" value, correct?

For example if the page originally defines: "h1 { color: red; }", my
"defaulter" stylesheet must say "defaultStyling h1 { color: black; }",
color being just one of the many attributes which could be inherited
for h1.

I'm guessing this would be quite a large stylesheet and I know it
would take a lot of time to make. I'd much prefer something
programmatic, if possible.

However, it is a viable solution and if worse comes to worse it'll be
the path I take and I thank you for it.

Any other ideas?

-sam

[snip]
[snip]

Jun 27 '08 #3
On Jun 5, 11:49 am, samm...@gmail.com wrote:
I'm going to hazard a guess that by *no* CSS, you mean the kind of
styling that you would get in the absence of a stylesheet.

Correct. Persnickety or not, good point. Let's just say I'd define
my own "defaults".

You suggest creating a stylesheet which defines all the "default"
styles that I'd be happy with for each classed element type
....
I'm guessing this would be quite a large stylesheet and I know it
would take a lot of time to make.
I actually don't think it would be a very large sheet in the end.
Maybe a couple hundred lines (and I'm including lines used for just
brackets and the formatting used to put a single property/value on a
line).
I'd much prefer something programmatic, if possible.

However, it is a viable solution and if worse comes to worse it'll be
the path I take and I thank you for it.
My other idea (which I haven't expressly tested for this
recommendation but I have used in the past to "sandbox" CSS is to use
an iframe. Content loaded into an iframe should not inherit styles of
the containing page and should be able to load its own CSS that won't
affect the elements of the page containing it.
This is not really a JS solution so I'm not sure if this list is the
right place to troubleshoot if you do pursue an iframe solution not
that it was a bad place for the initial inquiry.

- Dan Evans
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nntp | last post by:
I have a set of links which I want search engines to crawl them, but I want to disable them from my visitors, so I will ask the link owners to pay me to let me enable them. <a disabled...
2
by: techfuzz | last post by:
I scoured this group and others looking for the best way to disable a button after the first click to prevent multiple submissions, but never did find anything that worked like they said it would. ...
0
by: talal | last post by:
How to check whether javascript is enable or disable in client browser. Remeber i can check wheter browser client support javascript with BrowserCapability class. But what if the client has disable...
1
by: Bob | last post by:
Does anyone know how to disable javascript in the new Netscape 8 -- for off line testing (like can be done readily with the current MSIE, Firefox or Opera browsers) ? When I try tools -> options...
8
by: alamodgal | last post by:
hiiiiiiiiiii everybody, pls solve my problem if u can? Actualy what happens in my site when i press enter key it will show some secured...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.