spacer

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

home / experts / javascript / column93


Print Templates, Part IV: User's Settings

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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