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

  • Thread starter Thomas 'PointedEars' Lahn
  • Start date
T

Thomas 'PointedEars' Lahn

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

Mr said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top