This is as days pass by, by Stuart Langridge

And this is Shortest way to create an XMLHttpRequest object, written , and concerning JavaScript and the DOM

This should work in everything, I think, to get an XMLHttpRequest object for JavaScript in your browser: http = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null); Of course, this only applies if you're not using a library to make all this stuff go away, which you maybe should be. But if you're not, this is a very short way to get the thing you need for Ajax.

Comments

Shortest way to create an XMLHttpRequest object

[...] Original Article [...]

brothercake

What about try{} catch{} ? Don't you need to test-instantiate the object in IE to protect against runtime errors when ActiveX is disabled?

"which you maybe should be"

Why's that then..?

sil

brothercake: part of the reason it says "I think" up there is precisely because this is supposed to be the start of a conversation where we refine this to be the actual shortest way to do it :) Good point on ActiveX being present but disabled. I shall check that. If we have to use try/catch then we have to. Everyone's either using a library or inventing their own (similar but different) code to do this; it'd be nice to say "*this* is the right way, do it this way" :)

And on the libraries point, I refer the honourable gentleman to the answer I gave some moments ago :)

Matthew Somerville

I've always gone with Jim Ley: http://www.jibbering.com/2002/4/httprequest.html Note the extra XMLHTTP for a different version of MSXML; even copes with IceBrowser! On the downside, it definitely doesn't meet your shortest requirement... ;)

sil

Matthew: seeing that again was what prompted me to try and come up with a short and standard one that everyone can use :)

of our lives » Shortest way to create an XMLHttpRequest object

[...] Original post by sil   [...]

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.