Initializing an AJAX Application


XMLHttp = new XMLHttpRequest(); XMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); 

The basis of all AJAX applications is the aforementioned XMLHttpRequest object. All AJAX-enabled browsers support it natively, but in Internet Explorer the ActiveX object is required. There is one exception, though: Internet Explorer 7 comes with native XMLHttpRequest support. The best approach to create the object is to use TRycatch and to instantiate the native object first (to get Internet Explorer 7 on the right track even though this browser still supports ActiveX), and then the ActiveX version:

if (window.XMLHttpRequest) {   // instantiate native object } else if (window.ActiveXObject) {   // instantiate ActiveX object } 


Regarding the ActiveX object, there are several opportunities to instantiate it. The reason: Microsoft ships various versions of their XML library where this object is hidden. A bulletproof solution would be to check for all versions, using a convoluted piece of code. However, the following approach checks only the most important versions and works on Internet Explorer 5 onward and on all other AJAX-aware browsers:

Creating the XMLHttpRequest Object (xmlhttp.js)

function getXMLHttp() {   var XMLHttp = null;   if (window.XMLHttpRequest) {     try {       XMLHttp = new XMLHttpRequest();     } catch (e) { }   } else if (window.ActiveXObject) {     try {       XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");     } catch (e) {       try {         XMLHttp = new ActiveXObject(           "Microsoft.XMLHTTP");       } catch (e) { }     }   }   return XMLHttp; } 




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

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