15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 4 ✦ The Document and Body Objects (Chapter 18)<br />

Example<br />

See the discussion of the fireEvent() method in Chapter 15 of the <strong>JavaScript</strong><br />

<strong>Bible</strong> for an example of the sequence to follow when creating an event to fire on an<br />

element.<br />

createStyleSheet([“URL”[, index]])<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

Listing 18-13 demonstrates adding an internal and external style sheet to a document.<br />

For the internal addition, the addStyle1() function invokes document.<br />

createStyleSheet() and adds a rule governing the P elements of the page (not<br />

available for IE5/Mac). In the addStyle2() function, an external file is loaded. That<br />

file contains the following two style rules:<br />

H2 {font-size:20pt; color:blue}<br />

P {color:blue}<br />

Notice that by specifying a position of zero for the imported style sheet, the<br />

addition of the internal style sheet always comes afterward in styleSheet object<br />

sequence. Thus, except when you deploy only the external style sheet, the red text<br />

color of the P elements overrides the blue color of the external style sheet. If you<br />

remove the second parameter of the createStyleSheet() method in addStyle2(),<br />

the external style sheet is appended to the end of the list. If it is the last style sheet to<br />

be added, the blue color prevails. Repeatedly clicking the buttons in this example<br />

continues to add the style sheets to the document.<br />

Listing 18-13: Using document.createStyleSheet()<br />

<br />

<br />

document.createStyleSheet() Method<br />

<br />

function addStyle1() {<br />

var newStyle = document.createStyleSheet()<br />

newStyle.addRule(“P”, “font-size:16pt; color:red”)<br />

}<br />

function addStyle2() {<br />

var newStyle = document.createStyleSheet(“lst18-13.css”,0)<br />

}<br />

Continued<br />

245<br />

document.createStyleSheet()

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

Saved successfully!

Ooh no, something went wrong!