/    Sign up×
Community /Pin to ProfileBookmark

Create stylesheet in Firefox/Mozilla

This seems to work very easily in IE:

var ss = document.createStyleSheet(“/js/test/com/mentics/comp/TableTest.css”);
alert(“ss rules: “+document.styleSheets[1].rules.length);

But since Firefox/Mozilla doesn’t have createStyleSheet, what’s the right way to do this same thing in Firefox?

After some searching, I found:

[url]http://cse-mjmcl.cse.bris.ac.uk/blog/2005/08/18/1124396539593.html[/url]

And so tried this code:

var styles = “@import url(‘/js/test/com/mentics/comp/TableTest.css’);”;
var newSS=document.createElement(‘link’);
newSS.rel=’stylesheet’;
newSS.href=’data:text/css,’+escape(styles);
document.getElementsByTagName(“head”)[0].appendChild(newSS);
alert(“ss rules: “+document.styleSheets[1].cssRules.length);

But I get an error:

Error: uncaught exception: [Exception… “A parameter or an operation is not supported by the underlying object” code: “15” nsresult: “0x8053000f (NS_ERROR_DOM_INVALID_ACCESS_ERR)” location: “http://localhost/test Line: 2903″]

I’m using Firefox 1.5 beta 2.

Thanks!

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 15.2005 — if(document.createStyleSheet) {
document.createStyleSheet('../webdev.css');
}
else {
var styles = "../webdev.css";
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.type='text/css';
newSS.href=escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);
}
Copy linkTweet thisAlerts:
@jshellmanauthorOct 15.2005 — I tried that code before, and I just tried it again and I get the exact same error.

var styles = "/js/test/com/mentics/comp/TableTest.css";
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.type='text/css';
newSS.href=escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);


What browser did you test that in? I'm wondering if maybe there is a bug in Firefox 1.5 beta 2. If you could confirm that that code should work in firefox, I'll go over to the firefox forums and start asking if it's a bug in the beta version.

Thanks!
Copy linkTweet thisAlerts:
@FangOct 15.2005 — FF 1.0.7
Copy linkTweet thisAlerts:
@jshellmanauthorOct 15.2005 — Thanks for the response! I'm not sure what to do. I just installed Firefox 1.0.7 in a different directory and tried that code out and I still get that same error.

I have a full simple testcase now and I've attached it to this posting. I'm on Windows XP Pro. Could the OS version be causing the issue?

[upl-file uuid=3562d993-2f9b-403c-9b78-fa90a4a78e12 size=481B]test-dyncss.zip[/upl-file]
Copy linkTweet thisAlerts:
@FangOct 15.2005 — The problem is with style sheet access: http://www.quirksmode.org/dom/changess.html
Copy linkTweet thisAlerts:
@jshellmanauthorOct 15.2005 — What do you mean about style sheet access? I read through that page but I don't see where it talks about what I'm dealing with. If I try to access anything in cssRules in the new stylesheet I created I get that error.

Did you try my test case? Did it work for you?
Copy linkTweet thisAlerts:
@FangOct 16.2005 — Two problems, the [I]cssRules[/I] is specific to Moz. (W3C compliant). The link I gave explains the work around.

The error, in FF, is caused by requesting the rules before ending the function.

Add the style sheet, then request the rules in a separate function:<script>
onload = function()
{
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.type='text/css';
newSS.href=escape("test.css");
document.getElementsByTagName("body")[0].appendChild(newSS);
}
function CSSrules() {
var theRules = new Array();
if (document.styleSheets[0].cssRules) //Moz.
theRules = document.styleSheets[0].cssRules
else if (document.styleSheets[0].rules) // IE
theRules = document.styleSheets[0].rules
alert(theRules);
}
</script>
Opera can add a style sheet using this method, but you can not access it.
http://www.opera.com/docs/specs/js/dom/css/
Copy linkTweet thisAlerts:
@jshellmanauthorOct 17.2005 — I didn't see the workaround displayed on that link, but your explanation about having to "yield" before accessing the stylesheet makes sense. I stuck a setTimeout in between adding and accessing and it works just fine.

Thank you!
Copy linkTweet thisAlerts:
@FangOct 17.2005 — The section "cssRules[] and rules[]"
×

Success!

Help @jshellman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...