11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

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.

xmldoc.async = false;<br />

xmldoc.load("staff.xml");<br />

Obviously, with the use of an if statement we could make the previous example work in both<br />

browsers.<br />

if (window.ActiveXObject)<br />

var xmldoc=new ActiveXObject("Microsoft.XMLDOM");<br />

else if<br />

(document.implementation&&document.implementation.createDocument)<br />

xmldoc=document.implementation.createDocument("","doc",null);<br />

xmldoc.async = false;<br />

xmldoc.load("staff.xml");<br />

We leave it to the reader to make this modification to the previous example to make it crossbrowser.<br />

However, take notice of the fact that once again differences abound as Mozilla<br />

represents whitespace in its DOM tree and doesn‘t like to load the document initially. Just click<br />

the provided ―Redisplay XML Document‖ button.<br />

While it would seem from the previous paragraphs that Mozilla and IE aren‘t far apart, that isn‘t<br />

quite true as Mozilla provides the possibility of directly using XML and bringing in (X)HTML. IE<br />

can handle something like this but not very cleanly, as we‘ll see.<br />

In this particular example, we will use XML directly rather than XML accessed via an (X)HTML<br />

document. Because of this, we do not require a special XMLDocument object; instead, we<br />

reference the Document object just as we would expect. For example, to print out the<br />

nodeName property of the root element we would use<br />

alert(document.documentElement.nodeName)<br />

However, we need to bring in script to the XML document and then trigger it. <strong>The</strong>re is no easy<br />

way to do that in XML so we rely on (X)HTML tags such as form elements, as shown in this<br />

next example.<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!