Ajax - the XMLHTTPRequest code

Didn't find what you want? Try our search

Microsoft's Internet Explorer uses AvtiveX to create the XMLHTTPRequest object, whereas other borwsers - notable the Mozilla/Firefox range - allow the object to be created directly.

There are several ways of tackling this issue, some using browser detection routines (or tools such as BrowserHawk), others taking an approach similar to the following code, which tries all methods of creating the object until it discovers one that works:

try { req = new XMLHttpRequest(); /* e.g. Firefox */ } catch(e) { try { req = new ActiveXObject(\"Msxml2.XMLHTTP\"); /* some versions IE */ } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ } catch (E) { req = false; } } }

NOTE ON BROWSERS:
When you're experimenting with Ajax applications, it's important to check that your programs work in all of the commonly-used browsers. It always surprises me that so many Windows users don't install other browsers - at the very least, Firefox - to try in addition to Internet explorer. It's free, has pop-up blocking, tabbed browsing, as well as privacy and security features, and a rapidly-exoanding user base. If you run Windows, you can install Firefox alongside IE and compare the results in both. Why not take a moment to get it now?

See also the basic description of the XMLHTTPRequest object here and a more detailed discussion here.

 


Before using any software from this site, please see the Terms



8 Users Online.

All of the code and information on this site is provided free of charge (but without warranty).
If you feel that the site has been useful, you may care to donate a little toward the running of the site and the development of further projects?