J
JT
Asynchronous JavaScript
I am building a Web App which relies heavily on asynchronous
javascript (XMLHttpRequest).
It works a beautifully in IE7 and FF 2+ , but in IE6 (6.0.2900.2180
((sp2)) even though the asynchronous flag is set to true the call is
synchronous and doesn’t fork!! This problem is sporadic, only
happening on some peoples PCs some of the time and not others (with
the same full version as seen in Help->About). My code to test and
demonstrate this is as follows:.
The variables onExitRoot and callerHasLeft are used to test if the
Http Request was processed asynchronously..
Any ideas why this doesn’t work on some versions of IE6…
try
{
var onExitRoot = false;
var callerHasLeft = false;
var req = CTalk.getHTTPReq(); //get XMLHttpRequest or throw exception
if not supported
var reqStage1OK = false;
var to = setTimeout(function() { req.abort(); if(reqStage1OK)
CTalk.displayErrOnCon(CTalk._M_ERRCON_TYPE_INCOMPLETE); else
CUi.displayMsg("Error Connecting to X!",true); },
CTalk.respWaitASTol);
var url = "req.php?CID=" + escape(CTalk._M_CLIENT_ID) + "&M=" +
escape(mtype + param);
CUtil.log("opening ASync req: " + url);
req.open("GET", url, true);
req.onreadystatechange = function()
{
try
{
if(req)
{
CUtil.log("sendAsyncReq state : " + req.readyState);
if(req.readyState==1)
reqStage1OK = true;
else if(req.readyState==4)
{
if(req.status==200)
{
clearTimeout(to);
CUtil.log("HTTP 200 from sendAsyncReq, done.");
callback(req.responseText);
onExitRoot=true;
}
else
{
clearTimeout(to);
CUtil.log("HTTP/1.1 ERR from sendAsyncReq.");
try { CUi.displayMsg("Error",true); req.abort(); } catch(err) {}
onExitRoot=true;
}
}
}
} catch(e) { CUtil.log(e.message); alert("ERR: " + e.message); }
}
CUtil.log("About to send data request");
req.send(null);
if(onExitRoot && !callerHasLeft)
{
CUtil.log("Async not working!!");
//IF we get here async didn't work!!
//THis case is hit in IE6 on one call and then on a
subsiquent call it is not hit.
}
callerHasLeft = true;
} catch(e) { CUtil.log(e.message); alert("ERROR: " + e.message); }
I am building a Web App which relies heavily on asynchronous
javascript (XMLHttpRequest).
It works a beautifully in IE7 and FF 2+ , but in IE6 (6.0.2900.2180
((sp2)) even though the asynchronous flag is set to true the call is
synchronous and doesn’t fork!! This problem is sporadic, only
happening on some peoples PCs some of the time and not others (with
the same full version as seen in Help->About). My code to test and
demonstrate this is as follows:.
The variables onExitRoot and callerHasLeft are used to test if the
Http Request was processed asynchronously..
Any ideas why this doesn’t work on some versions of IE6…
try
{
var onExitRoot = false;
var callerHasLeft = false;
var req = CTalk.getHTTPReq(); //get XMLHttpRequest or throw exception
if not supported
var reqStage1OK = false;
var to = setTimeout(function() { req.abort(); if(reqStage1OK)
CTalk.displayErrOnCon(CTalk._M_ERRCON_TYPE_INCOMPLETE); else
CUi.displayMsg("Error Connecting to X!",true); },
CTalk.respWaitASTol);
var url = "req.php?CID=" + escape(CTalk._M_CLIENT_ID) + "&M=" +
escape(mtype + param);
CUtil.log("opening ASync req: " + url);
req.open("GET", url, true);
req.onreadystatechange = function()
{
try
{
if(req)
{
CUtil.log("sendAsyncReq state : " + req.readyState);
if(req.readyState==1)
reqStage1OK = true;
else if(req.readyState==4)
{
if(req.status==200)
{
clearTimeout(to);
CUtil.log("HTTP 200 from sendAsyncReq, done.");
callback(req.responseText);
onExitRoot=true;
}
else
{
clearTimeout(to);
CUtil.log("HTTP/1.1 ERR from sendAsyncReq.");
try { CUi.displayMsg("Error",true); req.abort(); } catch(err) {}
onExitRoot=true;
}
}
}
} catch(e) { CUtil.log(e.message); alert("ERR: " + e.message); }
}
CUtil.log("About to send data request");
req.send(null);
if(onExitRoot && !callerHasLeft)
{
CUtil.log("Async not working!!");
//IF we get here async didn't work!!
//THis case is hit in IE6 on one call and then on a
subsiquent call it is not hit.
}
callerHasLeft = true;
} catch(e) { CUtil.log(e.message); alert("ERROR: " + e.message); }