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
|