27.10.2015 Views

AJAX and PHP

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

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

Client-Side Techniques with Smarter JavaScript<br />

JavaScript <strong>and</strong> the Document Object Model<br />

As mentioned in Chapter 1, JavaScript is the heart of <strong>AJAX</strong>. JavaScript has a similar syntax<br />

to the good old C language. JavaScript is a parsed language (not compiled), <strong>and</strong> it has some<br />

Object-Oriented Programming (OOP) capabilities. JavaScript wasn't meant for building large<br />

powerful applications, but for writing simple scripts to implement (or complement) a web<br />

application's client-side functionality (however, new trends are tending to transform JavaScript<br />

into an enterprise-class language—it remains to be seen how far this will go).<br />

JavaScript is fully supported by the vast majority of web browsers. Although it is possible to<br />

execute JavaScript scripts by themselves, they are usually loaded on the client browsers together<br />

with HTML code that needs their functionality. The fact that the entire JavaScript code must arrive<br />

unaltered at the client is a strength <strong>and</strong> weakness at the same time, <strong>and</strong> you need to consider these<br />

aspects before deciding upon a framework for your web solution. You can find very good<br />

introductions to JavaScript at the following web links:<br />

30<br />

• http://www.echoecho.com/javascript.htm<br />

• http://www.devlearn.com/javascript/jsvars.html<br />

• http://www.w3schools.com/js/default.asp<br />

Part of JavaScript's power on the client resides in its ability to manipulate the parent HTML<br />

document, <strong>and</strong> it does that through the DOM interface. The DOM is available with a multitude of<br />

languages <strong>and</strong> technologies, including JavaScript, Java, <strong>PHP</strong>, C#, C++, <strong>and</strong> so on. In this chapter,<br />

you will see how to use the DOM with both JavaScript <strong>and</strong> <strong>PHP</strong>. The DOM has the ability to<br />

manipulate (create, modify, parse, search, etc.) XML-like documents, HTML included.<br />

On the client side, you will use the DOM <strong>and</strong> JavaScript to:<br />

• Manipulate the HTML page while you are working on it<br />

• Read <strong>and</strong> parse XML documents received from the server<br />

• Create new XML documents<br />

On the server side, you can use the DOM <strong>and</strong> <strong>PHP</strong> to:<br />

• Compose XML documents, usually for sending them to the client<br />

• Read XML documents received from various sources<br />

Two good introductions to DOM can be found at http://www.quirksmode.org/dom/intro.html<br />

<strong>and</strong> http://www.javascriptkit.com/javatutors/dom.shtml. Play a nice DOM game here:<br />

http://www.topxml.com/learning/games/b/default.asp. A comprehensive reference of the<br />

JavaScript DOM can be found at http://krook.org/jsdom/. The Mozilla reference for the JavaScript<br />

DOM is available at http://www.mozilla.org/docs/dom/reference/javascript.html.<br />

In the first example of this chapter, you will use the DOM from JavaScript to manipulate the<br />

HTML document. When adding JavaScript code to an HTML file, one option is to write the<br />

JavaScript code in a element within the element. Take the following HTML file<br />

for example, which executes some simple JavaScript code when loaded. Notice the document<br />

object, which is a default object in JavaScript that interacts with the DOM of the HTML page.<br />

Here we use its write method to add content to the page:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!