selectorText not supported in NS6/Mozilla
Ultimately, one of the methods our script provides (setStyleByTag()) was extremely difficult to implement in Netscape Navigator 6.0 or Mozilla, due to a lack of support for the selectorText property of the cssRules array. This lack of support is documented here.
We hope that the Mozilla project will remedy this quickly, as disabling selectorText has the unfortunate side effect of also excluding its value from cssRules objects altogether. It is possible to read the entire rule and scan for the selector. However Internet Explorer 5.0 gives you the full cssText value (albeit with case-folded properties):
SPAN { FONT-SIZE: 12pt; }
Navigator only gives the internal rule text, not the selector:
font-size: 12pt
Your only option is to grab the raw data from inside the STYLE tags and parse out your own alternate list of ruleset selectors, which is a pretty egregious kludge. This makes it difficult to process rulesets by selector, and since there is no standard capability for accessing rulesets by name (e.g., styleSheets[0].cssRules["H1"]) this missing feature of the DOM makes Netscape6/Mozilla far less powerful. I've included a workaround, based on code found in the Mozilla source, which creates an alternate list of selectors for comparison's sake, and which is only called in Netscape6/Mozilla.
Case doesn't matter in getAttribute() call
It doesn't appear to make any difference to either Macintosh browser whether you use
getAttribute("STYLE")
or
getAttribute("style")
for regular HTML documents, but depending on how an XHTML document is served, it may have an impact, as XHTML is case-sensitive (as are all XML documents).
Using the Script
The script contains several functions, discussed in detail below. The first three functions set styles, allowing you to discriminate between elements based on their ID, their CLASS, or their tag name. The second set of functions simply allows you to get the current style settings for each element.
The simplest of these is setStyleById(), which takes an element ID, a CSS property, and a value, and sets that element's property to that value.
setStyleById(ID, property, value)
The second function takes a tag name (e.g., H1), a class name, a CSS property, and a value, and sets the style for all elements of that type (or for all elements, if passed the "*" specifier).
setStyleByClass(type, class, property, value)
The third function takes a tag name (e.g., H1), a CSS property, a value, and the final (and optional) Boolean argument, which specifies whether to modify the style for that element in the stylesheet or in the .style array of the element. Unfortunately, due to the lack of a functional selectorText property in Netscape6/Mozilla, the stylesheet mode of the function does not work.
setStyleByTag(tagname, property, value, global)
The second set of functions allows you to read the values of any CSS property for any element, based on the element name, ID, or CLASS selector, as above.
getStyleById(ID, property)
getStyleByClass(type, class, property)
getStyleByTag(type, property)
If the property is found, it is returned as a string. If not, the functions return null.
A Demonstration of the Script
Note that because these scripts use advanced DOM functionality, they
will not work in any browser previous to the IE5/Netscape6/Mozilla
generation. This includes Navigator 4.x, Internet Explorer 4.x, and
other, older browsers. To ensure that your site provides for users
of those browsers, consider using a browser sniffer or server-side logic to deliver different content to those browsers.
Simply click on the buttons below to demonstrate the script.
Return to the JavaScript and CSS DevCenter.


 |
|