views:

499

answers:

3

We include the CSS file via standard HTML STYLE tag.

Then we need the javascript code to access the CSS classes and attributes. in IE and Chrome all goes fine, but in Firefox it throws this exception:

uncaught exception: Security error (NS ERROR DOM SECURITY ERR)

here's the javascript code:

for (var i = 0; i != window.document.styleSheets.length; i++) {
    rules = window.document.styleSheets.item(i);
    if(rules.href.indexOf('someurl.com')){
     break;
    }
}
return rules.cssRules || rules.rules;

it works fine in IE, Chrome and Safari, but it doens't in Firefox and Opera.

any ideas? thanks in advance

A: 

You can change styles with javascript with this:

elm.style.display = "none";

but those are write only, see quirksmode getStyles for reading styles.

It may depend on when and how you place your styles, using @import vs or placing your styles after your javascript, etc. certain race conditions can arise, so maybe try to get the styles after window.onload.

Tom
A: 

I'd say you are trying to change the style before it has loaded at all in FF. You could try moving the Javascript invocation to just before the closing tag, and also creating that method on the window.onLoad event or it's equivalent if you are using a JS Library.

Yaraher
+2  A: 

JavaScript can be referenced from any domain, but can only enact changes to the exact domain of the document that is executing it. By exact domain I mean everything from the protocol to just before the first directory must be identical.

You are not supposed to be able to use JavaScript to access a different domain. I am not sure why this is working in IE or Chrome, but it should not be. If the CSS is from domain different than the page executing the JavaScript you will be thrown a security error.

The real question here is: what property is item? I do not see that defined in your code and I have not seen that used before. Why are you trying to change the stylesheet with JavaScript instead of simply applying style changes directly to the DOM where security issues are not raised?