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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    cssText: Mozilla equivalent

    Hello,

    I am looking for the Mozilla equivalent of:

    Code:
    var sheet = document.styleSheets[1];
    sheet.cssText = styleString;
    where styleString is a collection of CSS rules.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    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 :
    Code:
    document.styleSheets[1].disabled=true;


    Code:
    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) {
            }
        }
    }

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by guvenck View Post
    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?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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