Section B.1. ajax.js


B.1. ajax.js

We put all this code in a file called ajax.js. You can call it anything you like, as long as you change your HTML <script> tag to refer to the right filename.

You've seen this code plenty of times by now. This little bit of JavaScript takes care of creating a request object, and makes sure that the object will work on all modern web browsers, from Internet Explorer to Firefox to Opera. Get used to this code if you're not already... it's the foundation of every Ajax application.

     var request = null; You'll see a lot of code that uses false here, instead of null, but that won't  work on some versions of IE. Stick with null, and your code will work on all platforms.     try {       request = new XMLHttpRequest(); Remember, you've got to try XMLHttpRequest for browsers like Opera and Mozilla,  and then try ActiveXObject for Internet Explorer.     } catch (trymicrosoft) {       try {         request = new ActiveXObject("Msxml2.XMLHTTP");       } catch (othermicrosoft) {         try {           request = new ActiveXObject("Microsoft.XMLHTTP");         } catch (failed) {           request = null; Follow the code... things end up here if all the different attempts at creating  a request object fail.         }       }     }     if (request == null)        alert("Error creating request object!"); OK, we admit it... this isn't the most robust way to handle errors. By now,  though, you're ready to add some more advanced error handling on your own. 





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net