23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

}<br />

addSheet("link", "eight.css");<br />

Figure 8–16. Dynamically including eight.css with addSheet()<br />

CHAPTER 8 ■ SCRIPTING CSS<br />

Did Firefox restyle your document with the blue sprite? Great. Now let’s again remove the <br />

with removeChild(). Then insert a with an @import directive with the help of addSheet(). Doing<br />

so restyles the document, too.<br />

function addSheet(tag, url) {<br />

var element = document.createElement(tag);<br />

element.type = "text/css";<br />

document.getElementsByTagName("head")[0].appendChild(element);<br />

if (tag === "link") {<br />

element.rel = "stylesheet";<br />

element.href = url;<br />

} else if (tag === "style") {<br />

try {<br />

element.sheet.insertRule("@import url(" + url + ")", 0);<br />

} catch (whyNot) {<br />

element.styleSheet.addImport(url);<br />

343

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

Saved successfully!

Ooh no, something went wrong!