PDA

View Full Version : Does the CSS style sheet belong to the DOM


webwarrior
01-09-2003, 06:14 PM
If so... how can I build a list of available styles?

I'm creating (extending) a DHTML rich text editor...

Thanks!

jkd
01-09-2003, 07:01 PM
What exactly do you mean?

document.styleSheets is a StyleSheetList which has all the StyleSheets in the document, collected from <?xml-stylesheet?>, or <link/> and <style>.

You can then use DOM2 CSS you grab all selectors, or rules, or whatever. Perhaps you should explain a little but further? :)

webwarrior
01-09-2003, 11:16 PM
Thanks, you gave me just enough input...

What I wanted to do was something like this:


<script language="JavaScript">
<!--
with (document.styleSheets[0])
{
for (i=0;i<rules.length;i++)
{
alert(rules.item(i).selectorText);
}
}
//-->
</script>


I'll take it from here!

(PS the above code is not PHP, but at least it looks fancy :cool: )

jkd
01-09-2003, 11:47 PM
You can make that DOM-compliant by using the cssRules collection instead of the IE-only rules collection.


<script language="JavaScript">

<!--

with (document.styleSheets.item(0))

{
var rls = cssRules || rules;

for (i=0;i<rls.length;i++)

{

alert(rules.item(i).selectorText);

}

}

//-->

</script>


Now that is DOM2 compliant (so it'll work in NS6+), and also functions in IE with that single shortcircuit workaround. :)

webwarrior
01-09-2003, 11:56 PM
Thanks!!

Although I hardly get any NS* clients to my sites... :(

jkd
01-10-2003, 12:42 AM
Originally posted by webwarrior
Thanks!!

Although I hardly get any NS* clients to my sites... :(

Well, this means that it'll work in NS6+, Konqueror and Safari (I believe KHTML does DOM2 Style... I'll have to check), and all future compliant browsers. It basically just guarentees you won't need to rewrite it for each new browser generation.

There are long-term benefits to writing DOM-compliant code, such as this.