14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

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

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

Chapter 8 ■ Back-End Interaction <strong>with</strong> <strong>Ajax</strong> <strong>and</strong> Node.js<br />

exampleExternalRSS.html (excerpt)<br />

<br />

<br />

Get Yahoo news<br />

<br />

<br />

<br />

■ ■Note RSS is an acronym for Really Simple Syndication. In essence, it is XML <strong>with</strong> content in it that you want to share <strong>with</strong><br />

the world—typically, news headlines. The specifications for RSS are available at http://blogs.law.harvard.edu/tech/rss,<br />

<strong>and</strong> you can read more about its benefits at Wikipedia: http://en.wikipedia.org/wiki/RSS.<br />

The important detail in this example is that RSS is a st<strong>and</strong>ardized format, <strong>and</strong> you know the XML structure—even<br />

if you get it from a third-party web site. Every valid RSS document contains—among many other things—an items<br />

element <strong>with</strong> nested item elements. Each of these contains at least a title describing <strong>and</strong> a link pointing to the full<br />

piece of information. You can use these to show a list of clickable headlines that send the user to the Yahoo site where<br />

she can read the full news article, as shown in Figure 8-6.<br />

Figure 8-6. Retrieving <strong>and</strong> showing RSS feed data<br />

The script is once again a simple XHR. The difference is that instead of linking to the URL directly, you pass it to<br />

the PHP script as a GET parameter:<br />

externalRSS.js<br />

readrss = {<br />

doxhr:function( container, url ) {<br />

[... code snipped as it is the same as in the last example ...]<br />

request.open('get', 'loadrss.php?url=' + encodeURI( url ) );<br />

request.setRequestHeader('If-Modified-Since', 'Mon, 12 Jan 2013 00:00:00 GMT' );<br />

262<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!