Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2003
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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...

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 DXHTML alternative to script-compiled CSS.

    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 Is a CSS rule a text node?

    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
    Last edited by brothercake; 05-05-2004 at 07:05 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •