AJAX and PHP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
I admit that the following piece of code may have puzzled you:<br />
if (!xmlResponse || !xmlResponse.documentElement)<br />
throw("Invalid XML structure:\n" + xmlHttp.responseText);<br />
Chapter 2<br />
Apparently, if xmlResponse is void, we risk generating another error when trying to read its<br />
documentElement property. In practice, the JavaScript interpreter only evaluates logical expressions<br />
when necessary, <strong>and</strong> it does so from left to right. In our particular case, if (!xmlResponse) is true,<br />
the second expression isn't evaluated at all, because the end result is true anyway. This feature,<br />
which is implemented in JavaScript <strong>and</strong> other languages, is called short-circuit evaluation <strong>and</strong><br />
you can read more about it here: http://www.webreference.com/javascript/reference/<br />
core/expr.html.<br />
Creating XML Structures<br />
XML <strong>and</strong> DOM are everywhere. In this chapter, you used the DOM to create HTML elements on<br />
the existing DOM object called document, <strong>and</strong> you also learned how to read XML documents<br />
received from the server. An important detail that we didn't cover was creating br<strong>and</strong> new XML<br />
documents using JavaScript's DOM. You may need to perform this kind of functionality if you<br />
want to create XML documents on the client, <strong>and</strong> send them for reading on the server.<br />
We won't go through more examples, but we will only show you the missing bits. The trick with<br />
creating a br<strong>and</strong> new XML document is creating the XML document itself. When adding elements<br />
to the HTML output, you used the implicit document object, but this is not an option when you<br />
need to create a new document.<br />
When creating a new DOM object with JavaScript, we're facing the same problem as with creating<br />
XMLHttpRequest objects; the method of creating the object depends on the browser. The following<br />
function is a universal function that returns a new instance of a DOM object:<br />
function createDomObject()<br />
{<br />
// will store reference to the DOM object<br />
var xmlDoc;<br />
// create XML document<br />
if (document.implementation && document.implementation.createDocument)<br />
{<br />
xmlDoc = document.implementation.createDocument("", "", null);<br />
}<br />
// works for Internet Explorer<br />
else if (window.ActiveXObject)<br />
{<br />
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");<br />
}<br />
// returns the created object or displays an error message<br />
if (!xmlDoc)<br />
alert("Error creating the DOM object.");<br />
else<br />
return xmlDoc;<br />
}<br />
After executing this function, you can use the created DOM object to perform whatever actions<br />
you want. For more details about creating the DOM object check the following link:<br />
http://www.webreference.com/programming/javascript/domwrapper/index.html. For details<br />
of using the DOM object, refer to the DOM articles mentioned earlier in this chapter.<br />
63<br />
www.it-ebooks.info