spacer
Yehuda Shiran September 24, 2001
Finding Style Rule Objects
Tips: September 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

You can access style definitions in their object form. The STYLE tags are represented in a zero-based collection, document.styleSheets. Each STYLE definition may include one or more rules. Each STYLE definition's rules are represented as a zero-based collection, document.styleSheets[i].rules. The style rule's name is stored as a property of the rules[j] object. The property name is selectorText. If your page looks like this:

<STYLE TYPE="text/css">
.contentstyle
{
    width:5.5in;
    height:8in;
    margin:1in;
    background:white;   
}
.masterstyle
{ 
    background:#FFFF99;
    border-left:1 solid black;
    border-top:1 solid black;
    border-right:4 solid black;
    border-bottom:4 solid black;
    width:8.5in;
    height:11in;
    margin:10px;
    overflow:hidden;
}
.headerstyle
{
    position:absolute;
    top:.25in;
    width:6in;
    left:1in;
}
.footerstyle
{
    position:absolute;
    top:10.5in;
    width:6in;
    left:1in;
}
</STYLE>
then you'll have four style rule objects: document.styleSheets[0].rules[0], document.styleSheets[0].rules[1], document.styleSheets[0].rules[2], and document.styleSheets[0].rules[3]. Their corresponding rule names can be accessed as follows:

document.styleSheets[0].rules[0].selectorText
document.styleSheets[0].rules[1].selectorText
document.styleSheets[0].rules[2].selectorText
document.styleSheets[0].rules[3].selectorText
The values of these properties are ".contentstyle", ".masterstyle", ".headerstyle", and ".footerstyle".

Suppose you want to store the above four objects in four object variables, oMasterStyleClass, oContentStyleClass, oFooterStyleClass, and oHeaderStyleClass, respectively. One way to do it is to go over all rule objects and search for the one you need, by name. The following findStyleRule() function does exactly that. It accepts a style rule name and returns the style rule object, styleSheets[i].rules[j]:

function findStyleRule(styleName) {
  for (i = 0; i < document.styleSheets.length; i++) { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) {
      if (document.styleSheets(i).rules(j).selectorText == styleName) {
        return document.styleSheets(i).rules(j);
      }
    }     
  }
}
The second line below returns the style rule object associated with the .masterstyle rule:

var oMasterStyleClass;
oMasterStyleClass = findStyleRule(".masterstyle");

People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business