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

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:
    470
  2. Oleg Ogurok

    Re: Web User Controls And StyleSheets

    Oleg Ogurok, Jun 30, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    695
    Steven Cheng[MSFT]
    Jul 8, 2004
  3. Logan
    Replies:
    2
    Views:
    495
    Dave Fancher
    Feb 1, 2005
  4. Cirene
    Replies:
    1
    Views:
    1,045
    Alexey Smirnov
    Jun 9, 2008
  5. JezB

    Properties, Styles, Stylesheets /////

    JezB, May 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    201
  6. Jason Ferguson

    Slide Show Object: Null or Not An Object Error

    Jason Ferguson, Feb 19, 2004, in forum: Javascript
    Replies:
    0
    Views:
    220
    Jason Ferguson
    Feb 19, 2004
  7. putty
    Replies:
    1
    Views:
    383
    putty
    Apr 5, 2005
  8. Tim Platt

    XMLHTTP - null is null or not an object

    Tim Platt, Jun 4, 2007, in forum: Javascript
    Replies:
    0
    Views:
    295
    Tim Platt
    Jun 4, 2007
Loading...