tags:

views:

666

answers:

2

document.styleSheets is used with an index,
but what If I want to use stylesheet.insertRule with with a specific CSS file ?

I know the file's name, which is injected to a page and at some point via JS.

A: 

Use this, and keep in mind:

For security reasons, Opera and Mozilla will not allow you to access the cssRules collection of a stylesheet from another domain or protocol. Attempting to access it will throw a security violation error

setStyleRule = function(selector, rule) {
 var stylesheet = document.styleSheets[(document.styleSheets.length - 1)];

 for( var i in document.styleSheets ){
  if( document.styleSheets[i].href && document.styleSheets[i].href.indexOf("myStyle.css") )
   stylesheet = document.styleSheets[i];
 }

 if( stylesheet.addRule ){
  stylesheet.addRule(selector, rule);
 } else if( stylesheet.insertRule ){
  stylesheet.insertRule(selector + ' { ' + rule + ' }', stylesheet.cssRules.length);
 }
}
vsync
A: 

Attention:

Security error" code: "1000 is also caused by Skype extension for Firefox. I do not know if there are Skype addons for other browsers but we know for sure that Skype extension for Firefox is causing a lot of problems especially in sites that use JavaScript to dynamically change styles.

Sorry for english.

Sebastian