New User -- "Null or not an object"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr X

    New User -- "Null or not an object"

    I'm a few weeks new to JS, and am having a problem. I have a simple INTERNAL style in a web page and I know that style is working. The problem arises when I try to run a script that changes one of that style's properties.

    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.class es.xtable" is null or not an object.

    ---------------------------------------------------
    function testerx()
    {
    document.classe s.xtable.all.co lor='Yellow'
    }
    --------------------------------------------------

    Both of these are in the <HEAD> section, and I've tested it with the style both before and after the script.

    Thsnks.

    ---Robert---
  • Thomas 'PointedEars' Lahn

    #2
    Re: New User -- &quot;Null or not an object&quot;

    Please do not post to Usenet in (Multipart) HTML format. Use a newsreader
    program if your Web interface is unable to create plain text messages (use
    alt.test or your favorite *test* group for tests). See news.newusers.i nfos.

    Mr X wrote:
    [color=blue]
    > 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.class es.xtable" is null or not an object.
    >
    > *---------------------------------------------------*
    > *function testerx()
    > {
    > document.classe s.xtable.all.co lor='Yellow'
    > }*
    > *--------------------------------------------------*[/color]

    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.classe s, 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.styleS heets[...].cssRules collection in the W3C DOM, where "..."
    refers to the zero-based index of the stylesheet. The IE DOM implements
    this as document.styleS heets[...].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.styleS heets[0].cssRules[0].style.color = "yellow";

    for UAs standards compliant in this regard like Mozilla

    document.styleS heets[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.styleS heets != "undefined"
    && (s = document.styleS heets)
    && 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

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Dynamic styles in stylesheets (was: New User -- &quot;Null or not an object&quot;)

      Please do not post to Usenet in (Multipart) HTML format, see
      news.newusers.i nfos and <http://insideoe.tomste rdam.com/>.

      Mr X wrote:
      [color=blue]
      > 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.class es.xtable" is null or not an object.
      >
      > *---------------------------------------------------*
      > *function testerx()
      > {
      > document.classe s.xtable.all.co lor='Yellow'
      > }*
      > *--------------------------------------------------*[/color]

      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.classe s, 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.styleS heets[...].cssRules collection in the W3C DOM, where "..."
      refers to the zero-based index of the stylesheet. The IE DOM implements
      this as document.styleS heets[...].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.styleS heets[0].cssRules[0].style.color = "yellow";

      for UAs standards compliant in this regard like Mozilla

      document.styleS heets[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.styleS heets != "undefined"
      && (s = document.styleS heets)
      && 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

      Comment

      Working...