Dynamic styles in stylesheets (was: New User -- "Null or not an object")

Discussion in 'Javascript' started by Thomas 'PointedEars' Lahn, May 1, 2004.

  1. Please do not post to Usenet in (Multipart) HTML format, see
    news.newusers.infos and <http://insideoe.tomsterdam.com/>.

    Mr X wrote:

    > The style is...
    > *--------------------------------------------------*
    > *.xtable, .xtable TD, .xtable TH
    > {
    > background-color:black;
    > color:white;
    > font-family:arial;
    > }*
    > *-------------------------------------------------*
    >
    > (the above style is workinh OK), but the following script gets the error
    > "document.classes.xtable" is null or not an object.
    >
    > *---------------------------------------------------*
    > *function testerx()
    > {
    > document.classes.xtable.all.color='Yellow'
    > }*
    > *--------------------------------------------------*


    You are using fantasy syntax and IE's error message is misleading
    one more time (see <http://jibbering.com/faq/>). There is no such
    property like document.classes, so this reference evaluates to
    `undefined' and the `undefined' literal value has indeed no `xtable'
    property as it has no properties at all.

    Instead, CSS selectors defined in a stylesheet are part of the
    document.styleSheets[...].cssRules collection in the W3C DOM, where "..."
    refers to the zero-based index of the stylesheet. The IE DOM implements
    this as document.styleSheets[...].rules. Provided that the stylesheet you
    posted is the first one that applies to the document and the selector you
    posted is the first one in that stylesheet, you can access its "color"
    property's value with

    document.styleSheets[0].cssRules[0].style.color = "yellow";

    for UAs standards compliant in this regard like Mozilla

    document.styleSheets[0].rules[0].style.color = "yellow";

    for IE. Be sure to check for DOM support prior to access:

    function testerx() // identifier should be more descriptive!
    {
    var s, r;
    if (typeof document != "undefined"
    && typeof document.styleSheets != "undefined"
    && (s = document.styleSheets)
    && typeof s.length != "undefined"
    && s.length > 0)
    {
    if (typeof s[0].cssRules != "undefined") // standards compliant
    {
    r = s[0].cssRules;
    }
    else (typeof s[0].rules != "undefined") // IE
    {
    r = s[0].rules;
    }
    if (r
    && typeof r.length != "undefined"
    && r.length > 0
    && typeof r[0].style != "undefined"
    && (s = r[0].style)
    && typeof s.color != "undefined")
    {
    s.color = "yellow";
    }
    }
    }

    See

    <http://pointedears.de.vu/scripts/test/whatami>

    for details.


    HTH

    PointedEars
    Thomas 'PointedEars' Lahn, May 1, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB
    Replies:
    2
    Views:
    398
  2. Logan
    Replies:
    2
    Views:
    343
    Dave Fancher
    Feb 1, 2005
  3. Cirene
    Replies:
    1
    Views:
    797
    Alexey Smirnov
    Jun 9, 2008
  4. JezB

    Properties, Styles, Stylesheets /////

    JezB, May 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    97
  5. putty
    Replies:
    1
    Views:
    246
    putty
    Apr 5, 2005
Loading...

Share This Page