...

View Full Version : cssText: Mozilla equivalent



guvenck
07-20-2007, 12:32 AM
Hello,

I am looking for the Mozilla equivalent of:



var sheet = document.styleSheets[1];
sheet.cssText = styleString;


where styleString is a collection of CSS rules.

rnd me
07-20-2007, 05:13 AM
this is an xbrowser version of what i think you want to do:
it adds a new style tag, so you may want to disable previous stylesheets (i noticed your example over-writes the last one...).
if that's intentional, right before you call adCSS, call :


document.styleSheets[1].disabled=true;







function addCSS(iCSS) {
var vi = "adds a css style section to the document, filled with text like an external stylesheet";
var D = document;
var h = D.getElementsByTagName("head");
if (!h.length) {
return;
}
var ns = D.createElement("style");
ns.type = "text/css";
h[0].appendChild(ns);
try {
ns.styleSheet.cssText = iCSS;
} catch (e) {
try {
ns.appendChild(D.createTextNode(iCSS));
ns.innerHTML = iCSS;
} catch (e) {
}
}
}

guvenck
07-20-2007, 02:29 PM
Hi, thanks for your answer.

Yes, that's intentionally. I generate a new stylesheet string (styleString) based on selections like color, font-size etc. from a form and replace the current stylesheet's content with it on the fly. Surprisingly, it is very easy on IE using cssText.

On Mozilla, I couldn't find an equivalent to do the same thing. I am adding LOTS of insertRule commands to accomplish this. It would shorten my code significantly, if there would be such a way to replace a stylesheet's content dynamically.

rnd me
07-20-2007, 05:06 PM
Hi, thanks for your answer.
It would shorten my code significantly, if there would be such a way to replace a stylesheet's content dynamically.

that's what the code i posted does.
you don't need any rule statements. just pass it a string that looks like a stylesheet, and it does the rest.

to replace, simply remove/disable the old style sections.

doing it rule by rule is to tedious for me, what with the xbrowser issues and all.

is there something i left unclear?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum