Monday, June 15, 2009

IE6 Ajax Problem with included .js file

Hi All,

Recently i got to make a site that was to be used globally in different browsers, i made this site using jsp and other bunch of technologies and frameworks. The site also has to be compactible to all known browsers world wide including blackberry and safari for iphone. On during the development stage i found a very strange problem in internet explorer 6, i included a javascript file for ajax and created some of its instances in calling page javascript code, the browser behaved very strangely,

It sometimes was able to create an instance of that ajax object and sometimes was not able to create an instance. Well, in ie7 the code worked like a butter. I finally concluded of not using that included javascript code and directly using that function code in the calling jsp page.

Check out the below scenerio:

*********My ajax.js file
/*
This function returns XmlHttpRequest Object for ajax support
*/
function getXmlHttpObject(){

//identify the browser
if (window.XMLHttpRequest) {
//alert(" you are using IE7 or mozilla");
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
if (xmlHttp.AJAX){
alert('firefox ajax support found');
}

}catch (e){
// Internet Explorer
var versions = ['Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
for (var i = 0; i < versions.length ; i++){
try {
xmlHttp.AJAX = new ActiveXObject(versions[i]);
if (xmlHttp.AJAX){
alert('internet explorer ajax support found');
break;
}
}catch (objException) {}
}
}
return xmlHttp;

} else {
//alert(" you are using IE6");

try {
//alert("hello am in");
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
//alert("hello am out");

if (xmlHttp == null){
alert('internet explorer ajax support not found');
}
}catch (objException) {}
return xmlHttp;
}
}



****************My.jsp (The calling page)



function checkAjaxInIe()
{
var xmlHttp = getXmlHttpObject();
if (xmlHttp == null)
{
alert("Your browser does not support AJAX!");
return;
}

}


After implementing this scenerio, try running this code on ie6 with script debugger activated, if you run this code 100 times, you will get probability of the calling page creating instance of included js file only 50 times from 100 times.

To resolve this, i simply included the included file's ajax function in all my jsps.

Cheers!!!

Ujjwal B Soni