23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Adding a Rule to a Style Sheet<br />

CHAPTER 8 ■ SCRIPTING CSS<br />

Now that we have a helper function to provide us with the numeric index of a rule, we can explore how<br />

to insert a rule into a style sheet. Of course, there is a DOM way and an Internet Explorer way. Let’s write<br />

another helper function to compensate for Internet Explorer’s skullduggery.<br />

Clear Firebug, but do not refresh Firefox because we want findIndex() to remain in memory. Name<br />

the helper function insertRule. This one will work with four parameters:<br />

• element will be a or Element node.<br />

• selector will be the text of the selector for the new rule, in other words, a string<br />

like "div#running li".<br />

• declarations will be the text of the declaration block, minus the curly braces, such<br />

as a string like "top:135px; background-position:0 -81px". Note that property<br />

names are dash case. It’s just like in your CSS code or in<br />

CSSStyleDeclaration.cssText.<br />

• index contains the text of the selector for the rule you want to insert the new rule<br />

before. So, that’s the selector string we will pass to findIndex(), which will then<br />

return a numeric index, or undefined if we are out of luck.<br />

function insertRule(element, selector, declarations, index) {<br />

}<br />

Now let’s move on to the body of our helper function. Assign either the DOM or Internet Explorer<br />

member that contains the CSSStyleSheet object to a local variable named sheet. Similarly, assign the<br />

DOM or Internet Explorer member that contains the CSSRuleList array-like object to one named rules.<br />

It’s just like we did in findIndex().<br />

Now let’s make the index parameter optional by way of a couple if statements. JavaScript will run<br />

the block of the first if statement in the event that we did pass a selector string for the value of index.<br />

Otherwise, JavaScript will run the block of the second if statement, like if we invoked insertRule() with<br />

just three parameters rather than four. In this case, index defaults to undefined. Let’s overwrite that value<br />

with rules.length, which contains a number one greater than the total number of rules in the style<br />

sheet. Later, this numeric index will enable us to append the new rule to the very end of the style sheet.<br />

Thus far we have this:<br />

function insertRule(element, selector, declarations, index) {<br />

var sheet = element.sheet || element.styleSheet;<br />

var rules = sheet.cssRules || sheet.rules;<br />

if (typeof index === "string") {<br />

index = findIndex(element, index);<br />

}<br />

if (typeof index !== "number") {<br />

index = rules.length;<br />

}<br />

}<br />

Now sheet will contain either a DOM method named insertRule() or an Internet Explorer method<br />

named addRule(). Let’s figure out which one is available by way of the else if idiom. In the event that<br />

Firefox, Safari, or Opera is running our function, insertRule() will be defined. This method takes two<br />

parameters:<br />

• The full text of the rule, curly braces and all. So, we will cobble that together with<br />

the + operator.<br />

329

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!