Thread: IE8 ajax asynchronous racing condition
the below ajax code works on browsers except ie8 works 50% of time.
works in asynchronous mode when php code returns data enough.
when not fast enough, ignores wait function , returns data after
code hits "return true".
annoying when using predictive text on html input box.
problem can solved inserting alert thats not ideal.
thoughts appreciated.
code:<html> <head> <script type="text/javascript"> var gbonload = false; function gethttpresponse(psurl, pofunc, pbissynchronous) { var fiisie8plus = window.xdomainrequest ? true : false; var http = getxmlhttpobject(); if (fiisie8plus) { gbonload = false; http.ontimeout = function() { alert("please try again. on repeated display of message please contact webmaster"); }; http.onerror = function() { alert("an error ocurred. please try again or contact webmaster"); }; http.onprogress = function() {}; http.onload = function() { pofunc(http); gbonload = true; }; } else { if ( pbissynchronous == false ) { http.onreadystatechange=function() { if(http.readystate == 4) { if(http.status == 200) { pofunc(http); } } } } } if ( pbissynchronous == false ) { http.open("get", psurl, true); http.send(null); } else { if (fiisie8plus) { http.open("get", psurl, true); http.send(null); // doesnt work; there seems race condition (50% of time) can fixed alert or prompt // if (gbonload == false) { // waitfordata(); // } } else { http.open("get", psurl, false); http.send(null); pofunc(http); } } } function getxmlhttpobject() { var foxhr = 0; var fiismsie = /*@cc_on!@*/0; if (fiismsie) { (fiidx=1;fiidx<=8;fiidx++) { try { if ( fiidx == 1 ) { foxhr = new xdomainrequest(); break; } if ( fiidx == 2 ) { foxhr = new activexobject("msxml2.xmlhttp.6.0"); break; } if ( fiidx == 3 ) { foxhr = new activexobject("msxml2.xmlhttp.5.0"); break; } if ( fiidx == 4 ) { foxhr = new activexobject("msxml2.xmlhttp.4.0"); break; } if ( fiidx == 5 ) { foxhr = new activexobject("msxml2.xmlhttp.3.0"); break; } if ( fiidx == 6 ) { foxhr = new activexobject("msxml2.xmlhttp.2.0"); break; } if ( fiidx == 7 ) { foxhr = new activexobject("microsoft.xmlhttp"); break; } if ( fiidx == 8 ) { foxhr = window.createrequest(); break; } } catch( e1 ) { } } } else { (fiidx=1;fiidx<=2;fiidx++) { try { if ( fiidx == 1 ) { foxhr = new xmlhttprequest(); break; } if ( fiidx == 2 ) { foxhr = window.createrequest(); break; } } catch( e1 ) { } } } return foxhr; } function pause(pimilliseconds) { var fodate = new date(); while ( (new date()) - fodate <= pimilliseconds) { // nothing } } function waitfordata() { // wait time in milliseconds var fimilliseconds = 100; // wait 30 seconds. work out number of loops needed , return int var fiwaitmaxsecond = ((30 * 1000 ) / fimilliseconds) | 0; (var fiidx=0; fiidx<fiwaitmaxsecond; fiidx++) { if (gbonload == true) { break; } pause(fimilliseconds); } } function callurlone() { var fofunc = function(pohttp) { var fodivid = document.getelementbyid("one"); fodivid.innerhtml = pohttp.responsetext; } gethttpresponse('test.php', fofunc, true); // code here return true or false return true; } </script> </head> <body> <!-- suspect race condition created return --> <button type="submit" onclick="return callurlone();">one</button> <div id="one"> test </div> <div id="two"> </div> </body> </html>php code:<?php
// ie needs the header to work
header('access-control-allow-origin: *');
echo "data";
?>
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk IE8 ajax asynchronous racing condition
Ubuntu
Comments
Post a Comment