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 10 ■ SCRIPTING BOM<br />

<br />

<br />

<br />

<br />

<br />

However, we receive this as a string:<br />

"<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

"<br />

So, we’re going to have to write a function to search through the string and create the , ,<br />

, and Element nodes before we can place them on the page, making sure of course to get the<br />

nesting right.<br />

Because this is the final chapter in the book, you probably can roll a helper function to do the job by<br />

yourself. So I’ll leave that, as they say, as an exercise for the reader.<br />

Just kidding. You’d likely gnaw off a finger or two in frustration trying to code that.<br />

It’d be pretty dull to explain, too. Turns out, I won’t have to.<br />

Internet Explorer 4 gave every Element node a proprietary innerHTML member. If you assign a string<br />

to innerHTML, JavaScript parses it into HTML and then replaces all descendents of the Element node with<br />

that DOM branch. That may be draconian, but it’s practical, too. It’s so much so that Firefox, Safari, and<br />

Opera have always implemented innerHTML, even though it’s not part of any DOM standard.<br />

Anyway, innerHTML is totally perfect for parsing an HTML response, quietly converting it from a<br />

string value to a branch of the DOM tree. I guess we’ll use it then.<br />

Here’s how: XMLHttpRequest.responseText contains the string equivalent of the HTML in<br />

data/s2.html. parseHTML() will assign that to innerHTML for . However, we’re<br />

going to need to create that and the other elements of the scroller first with our helper function,<br />

createElem()—but only if we received data/s2.html all right from the server or browser cache. To make<br />

sure of that we test whether XMLHttpRequest.status is 200 (received data/s2.html from the server) or 304<br />

(received data/s2.html from the cache).<br />

With all those details spilling out of mind, let’s begin coding parseHTML() like so. Note that the req<br />

parameter will contain the XMLHttpRequest object passed in by getData() when<br />

XMLHttpRequest.readyState changes to 4:<br />

function parseHTML(req) {<br />

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

}<br />

}<br />

Note that if you are testing this script on your computer, which is to say loading URLs with the<br />

file:// protocol, there obviously will not be an http:// status code. So, XMLHttpRequest.status will<br />

always be 0, no matter what. With this in mind, if you are testing the script on your computer, you must<br />

replace 200 or 304 with 0. Otherwise, the if block will never run!<br />

429

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

Saved successfully!

Ooh no, something went wrong!