spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column93


Print Templates, Part IV: User's Settings

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

Converting Style Rules to Objects

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 of the 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. Our print template includes the following STYLE definition:

<STYLE TYPE="text/css">
.contentstyle
{
  width:5.5in;
  height:8in;
  margin:1in;
  background:white;
  border:1 dashed gray;
}
.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:11.0in;
  width:6in;
  left:1in;
}
</STYLE>

We 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".

We 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);
}
}
}
}

Here is how we call this function to convert the style definitions to style objects:

var oMasterStyleClass;
var oContentStyleClass;
var oHeaderStyleClass;
var oFooterStyleClass;

oMasterStyleClass = findStyleRule(".masterstyle");
oContentStyleClass = findStyleRule(".contentstyle");
oHeaderStyleClass = findStyleRule(".headerstyle");
oFooterStyleClass = findStyleRule(".footerstyle");

Next: How to overwrite the style definitions with the user's settings

http://www.internet.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: September 24, 2001
Revised: September 24, 2001

URL: http://www.webreference.com/js/column93/3.html