Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2004, 03:57 AM   PM User | #1
guoshuang
New to the CF scene

 
Join Date: Sep 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
guoshuang is an unknown quantity at this point
How to use CreateStyleSheet(stylehere)

i have read this

http://msdn.microsoft.com/workshop/a...stylesheet.asp

but i can not understand this

If the URL contains style information, this information will be added to the style object.

like this?

document.createStyleSheets("<style>body {color:red}</style>")
or
document.createStyleSheets("body {color:red}")

all these two can not work.can anybody help me to write the right one?

thanks...
guoshuang is offline   Reply With Quote
Old 05-05-2004, 06:37 AM   PM User | #2
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
Neither of those things. Try this:
Code:
var sheet = document.createStyleSheet();
sheet.addRule('body','color:red');
All they're saying with "this information will be added to the style object" is that if you go like this:
Code:
var sheet = document.createStyleSheet('whatever.css');
sheet.addRule('body','color:red');
It will load all the existing rules from that stylesheet, and then add the new rule to it. Whether they mean it will be added to the computed style or literally added to .style - I don't know - one would hope the former, but knowing IE it might be the latter.

BUT - here's the big but - all of this is IE proprietary junk. Loosely similar to DOM 2 CSS (the standard from which it's derived) but it abuses the rule syntax by forcing you to write selector/property pairs, rather than inserting single rules which is (imo) far more logical and in practise far more useful.

But DOM 2 CSS is not complete either - although it allows you to create a stylesheet and insert rules, it provides no mechanism for associating the newly-created stylesheet with the document. But you can still make it work by: creating a <style> node with createElement, then finding it in the document.styleSheets collection, then you have an object into which you can insert rules. This only works in Mozilla (afaik); more info at http://www.codingforums.com/showthread.php?t=18442.

What you can also do is create a <style> node and then insert CSS rules as text nodes - which works in Opera 7.5 and Mozilla builds other than very old ones (<1.0) Dunno about IE. Info at http://www.codingforums.com/showthread.php?t=29314

I guess you could use a combination of that and IE's proprietary model, but unless you need to create the stylesheet after load time, you may as well use document.write for IE ... what I do is compile rules in an array at load time - since each array item is a complete rule I can just iterate through it and use multiple techniques - insert a rule, or create and insert a text node, or write another line out with document.write
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 05-05-2004 at 07:05 AM..
brothercake is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:11 PM.


Advertisement
Log in to turn off these ads.