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.

CHAPTER 10 ■ SCRIPTING BOM<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

The only problem is it’s floating around in memory, totally invisible to visitors. So, we want to insert<br />

it into the DOM tree with appendChild(), a method we covered in Chapter 7:<br />

function parseHTML(req) {<br />

if (req.status === 200 || req.status === 304) {<br />

var div = createElem("div", {className: "scroller", id: "s2"}, [<br />

createElem("div", {className: "wrapper"}),<br />

createElem("div", {className: "left arrow sprite"}),<br />

createElem("div", {className: "right arrow sprite"})]);<br />

div.firstChild.innerHTML = req.responseText;<br />

document.body.appendChild(div);<br />

}<br />

}<br />

Regardless of the HTTP status code for our GET request for data/s2.html, we want to call a function<br />

named prep(), which will replace the function literal we currently have for the load event. That way, if<br />

we get an undesirable status code, say a 404 “Not found” for mistyping the URL, prep() will still run,<br />

adding the drag and drop, sprite, and other behaviors elements on our page.<br />

function parseHTML(req) {<br />

if (req.status === 200 || req.status === 304) {<br />

var div = createElem("div", {className: "scroller", id: "s2"}, [<br />

createElem("div", {className: "wrapper"}),<br />

createElem("div", {className: "left arrow sprite"}),<br />

createElem("div", {className: "right arrow sprite"})]);<br />

div.firstChild.innerHTML = req.responseText;<br />

document.body.appendChild(div);<br />

}<br />

prep();<br />

}<br />

Underneath parseHTML(), define a function named prep(). In its body, call prepSprites(),<br />

prepDrag(), prepSkinKeys(), and prepScrollers(). Finally, in the body of the load event listener, which<br />

now contains just a call to presetSkin(), append a call to getData() to fetch data/s2.html. Our script<br />

now ends like so:<br />

function prep() {<br />

prepSprites();<br />

prepDrag();<br />

prepSkinKeys();<br />

prepScrollers();<br />

}<br />

addListener(window, "load", function(e) {<br />

presetSkin();<br />

getData("data/s2.html", parseHTML);<br />

431

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

Saved successfully!

Ooh no, something went wrong!