Tuesday, June 6th, 2006

Ajax on IE 7: Check native first

Category: IE, Tip

I would hasten a guess that there is a lot of code out there that starts off with:

javascript

  1. /*@cc_on
  2.         @if (@_jscript_version >= 5)
  3.                 try {
  4.                         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  5.                 } catch (e) {
  6.                         try {
  7.                                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  8.                         } catch (E) {
  9.                                 xmlhttp = false;
  10.                         }
  11.                 }
  12.         @else
  13.         xmlhttp = false;
  14.         @end @*/
  15.         if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
  16.                 try {
  17.                         xmlhttp = new XMLHttpRequest();
  18.                 } catch (e) {
  19.                         xmlhttp = false;
  20.                 }
  21.         }
  22.         return xmlhttp;

and what is wrong with this? Well, apart from being pretty verbose, in the world of IE7 we should be invoking XMLHttpRequest() first and having the ActiveX control done only if window.XMLHttpRequest isn’t around to play.

Posted by Dion Almaer at 8:55 am
11 Comments

+++--
3.6 rating from 37 votes

11 Comments »

Comments feed TrackBack URI

Only problem is that we’re not going to see IE7 widespread for some time now.

Comment by Tom — June 6, 2006

Yeah I don’t really think IE7 will be widespread until 2008 anyways. Sure Vista is due to be released sometime in 2007, I say sometime because who knows if it will make its current date. But even after its released not everyone will jump at it and go buy a new PC, that will take a few months to a few years.

So while it might be a good idea to prepare for IE7 I would’nt stress over it, lets wait until the night before in true procrastinat…I mean in the programmer fassion.

Comment by pplante — June 6, 2006

This has been covered like, a bajillion times, besides everything i see has
if (typeof XmlHttpRequest == “undefined”)
first anyway.

yes, I said bajillion

Comment by Allen Rice — June 6, 2006

why not just simplely adopt a library , prototype.js for example ?

forgot those trivial tricks, the life will be much easy

Comment by chenggn — June 6, 2006

if (!window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
return new ActiveXObject(“Microsoft.XMLHTTP”);
};
}

Comment by Dean Edwards — June 6, 2006

i like that last comment

Comment by Dave Ashe — June 7, 2006

There are reasons to worry about it now. Imagine an IE7 Ajax implementation that submits a credit card order. Do you want that to be submitted over and over again without knowing about it?? See Brent’s writeup on how IE7 behaves if the ActiveX control is used.

As for chenggn’s comment, some of the libraries did not try to instantiate the native object first. Of the few I’ve checked they are doing it right now.

Comment by Tim A — June 7, 2006

i think the ie7 is more widespread after the release of vista in january 2007. most users that have an pc which is bought in 2006 can upgrade to vista smoothly, ’cause most driver will be availble till mai 2007 i think. my notebook is from the end of 2005 (acer 8104). most driver will work without downloading anything from acer and sure i will upgrade since i develop also software on new technics.

cheers, murphy

Comment by murphy — June 12, 2006

I got a problem with this code, when i try to compress my js file, as you have to keep this part before compressing your js file

/*@cc_on
@if (@_jscript_version>= 5)
try {
xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/

just a follow up for the code above.

Comment by Isulong SEOPH — June 14, 2006

Internet expler 7 + path.

if cant instal change xp serial.
too Windows XP Corp vlk(Volume License Key)

sory for my english.

serial:
http://rapidshare.de/files/25304013/serial_xp.zip
0.05 MB

http://rapidshare.de/files/25304192/microsoft_internet_explorer_7_crack.zip
13.16 MB

Comment by mario — July 8, 2006

Please read http://xhab.blogspot.com/2006/11/ie7-support-for-xmlhttprequest.html
It explains the disadvantages when using the site locally unter the “file:” protocol!

Comment by Kevinin — May 4, 2007

Leave a comment

You must be logged in to post a comment.