Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Dynamic styles in stylesheets (was: New User -- "Null or not an object")

Reply
Thread Tools

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

 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-01-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i use STYLESHEETS to set styles for ASP.NET controls?? Logan ASP .Net 2 02-01-2005 05:04 AM
Re: Web User Controls And StyleSheets Oleg Ogurok ASP .Net 7 07-08-2004 03:30 AM
Properties, Styles and StyleSheets for a web control JezB ASP .Net 2 05-17-2004 08:22 AM
Properties, Styles, Stylesheets ///// JezB ASP .Net Web Controls 0 05-14-2004 02:48 PM
Re: user control stylesheets Michael Mckenzie ASP .Net 0 04-22-2004 10:06 PM



Advertisments