XMLHttpRequest IE vs FireFox

S

shankwheat

I use this code to populate a selectbox with a group of records from a
database. It executes very quickly with FireFox 2.0 but takes 7-10
secs with IE6 and IE7. Just wondering if anyone had any pointers for
improving this code to make IE run it faster or I am just stuck with
it? Thanks

function GetXmlHttpObject()
{
var objXMLHttp=null

try {
objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
} catch (e) {
try {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
} catch (e) {
objXMLHttp = null;
}
}

if (objXMLHttp==null)
{
objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari
}
return objXMLHttp
}


function getCompanies()
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support the XMLHttpRequest object.")
return
}

var url="getCompanies.aspx"
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
document.getElementById("lblResults").innerHTML = "<img
src='mozilla_blu.gif' alt='loading..please wait'>"; //loading
}


function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("lblResults").innerHTML = "";

// Clear the available listbox of any previous options
document.choiceForm.available.length = 0;

// Split the delimited response into an array
var s = xmlHttp.responseText;
var re = new RegExp('{([^{}]*)}','g');
var x = [];
while (re.exec(s)){
x.push(RegExp.$1.split('#'));
}

// Add the array options to the selectbox available
var sel = document.forms['choiceForm'].elements['available'];
for (var i=0, len=x.length; i<len; i++){
sel.options = new Option(x[1], x[0]);
}
}
}
 
M

Martin Honnen

shankwheat said:
I use this code to populate a selectbox with a group of records from a
database. It executes very quickly with FireFox 2.0 but takes 7-10
secs with IE6 and IE7. Just wondering if anyone had any pointers for
improving this code to make IE run it faster or I am just stuck with
it? Thanks

function GetXmlHttpObject()
{
var objXMLHttp=null

try {
objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
} catch (e) {
try {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
} catch (e) {
objXMLHttp = null;
}
}

if (objXMLHttp==null)
{
objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari

Unless an IE 7 user has scripting of ActiveX objects disabled your code
will never end up here with IE 7. Thus if you want your code to use
XMLHttpRequest with IE 7 you should check
if (typeof XMLHttpRequest != 'undefined') {
objXMLHttp = new XMLHttpRequest();
}
first, then if that fails you should use the new ActiveXObject code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top