jQuery
New Wave Javascript
discuss » AJAX plugin does not work in IE6
Posted: Wed Mar 1 14:38:47 EST 2006
From: John Resig <
jeresig at gmail.com
>
Hmmm, yeah, that seems like a smart way to go about it - make things a
little bit quicker for everyone, I'd imagine. Alright, I'll see if I
can add this in, soon.
--John
On 3/1/06, Jim Dabell <
jim-jquery.com at jimdabell.com> wrote:
>
>
> So, I've adjusted the code to no longer use them, pushed the code live
>
> - and I can confirm that it works in both browsers. You can check it
>
> for yourself, here:
>
> http://jquery.com/demo/ajax/
>
>
I've had a look at the code, and have a couple of suggestions.
>
>
Firstly, according to Yahoo's library¹, there's more than just two ProgIds to
>
try and instantiate.
>
>
Secondly, you might be able to make the code a bit smaller and quicker by
>
checking for a native XMLHttpRequest constructor and generating an
>
XMLHttpRequest function when it isn't there that just tries to instantiate
>
the ActiveX objects.
>
>
Something like the following (untested) should work:
>
>
var progId = null;
>
var progIds = [
>
'MSXML2.XMLHTTP.5.0',
>
'MSXML2.XMLHTTP.4.0',
>
'MSXML2.XMLHTTP.3.0',
>
'MSXML2.XMLHTTP',
>
'Microsoft.XMLHTTP'
>
];
>
>
if (typeof XMLHttpRequest == "undefined") {
>
function XMLHttpRequest() {
>
var obj = null;
>
if ( progId ) {
>
return new ActiveXObject(progId);
>
}
>
for (var i = 0, id; id = progIds[i]; ++i) {
>
try {
>
obj = new ActiveXObject(id);
>
progId = id;
>
return obj;
>
} catch ( e ) {}
>
}
>
return null;
>
}
>
}
>
>
Then .xml() can be simplified to:
>
>
$.xml = function( type, url, data, ret ) {
>
type = type || "GET";
>
var xmlhttp = new XMLHttpRequest();
>
>
if ( !xmlhttp ) return;
>
>
xmlhttp.open(type, url, true);
>
>
if ( data )
>
xmlhttp.setRequestHeader('Content-Type',
>
'application/x-www-form-urlencoded; charset=UTF-8');
>
>
if ( ret )
>
xmlhttp.onreadystatechange = function() {
>
if ( xmlhttp.readyState == 4 ) ret(xmlhttp);
>
};
>
>
xmlhttp.send(data)
>
};
>
>
This is just off the top of my head to give you an idea of what I mean, you
>
might have to bash it a bit to get it working.
>
>
This means that Internet Explorer doesn't check for a native implementation
>
every time it tries to instantiate an XMLHttpRequest object, and it remembers
>
which ProgId is used so it doesn't have to search for the right one each
>
time.
>
>
¹ Yahoo's XMLHttpRequest implementation (BSD licensed):
>
<http://developer.yahoo.net/yui/connection/docs/overview-summary-connection.js.html>
>
<http://developer.yahoo.net/yui/license.txt>
>
>
--
>
Jim
>
>
_______________________________________________
>
jQuery mailing list
>
discuss at jquery.com
>
http://jquery.com/discuss/
>
--
John Resig
http://ejohn.org/jeresig at gmail.com