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.

CHAPTER 8 ■ SCRIPTING CSS<br />

342<br />

Figure 8–15. Delete the element with removeChild(), and Firefox redisplays the document as<br />

unstyled markup.<br />

By the way, even though it may be obvious, note that passing a or to removeChild()<br />

does the inverse of what addSheet() does. So, removing a style sheet is simpler than adding one.<br />

Now that our document is totally unstyled, we can tangibly test addSheet(). Let’s first try to include<br />

eight.css with a new . Go ahead and delete the removeChild() invocation. Then call addSheet()<br />

like so, verifying your work with Figure 8–16.<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 />

}

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

Saved successfully!

Ooh no, something went wrong!