javascript - INVALID_STATE_ERR: DOM Exception 11 -


ok, i've created working javascript ajax file, generates absurd number of these dom exceptions. i'm not sure why is, because can see, elements call still in existance. code here:

window.onload = function(){init();}  function init() {     ajax = ajaxinit();     setinterval(function(){ajaxcontact(ajax);},2000);     ajaxcontact(ajax);     ajax.onreadystatechange = function() {update(ajax);} }  function ajaxinit() {     if (window.xmlhttprequest) {       ajax = new xmlhttprequest();     }     else {         if (window.activexobject) {               ajax = new activexobject("microsoft.xmlhttp");         }     }     if (ajax) {         document.getelementbyid("status").innerhtml = "ajax initialized";         return ajax;     }     else {         docuement.getelementbyid("status").innerhtml = "error: ajax not available";         return false;     }  }  function ajaxcontact(ajax) {     try {         ajax.open("get","updateajax.php?" + "ran=" + math.random(),true);         ajax.send();     }     catch (err) {         alert(err.message);         document.getelementbyid("status").innerhtml = "error contacting server";         document.getelementbyid("loading").src = "images/redx.png";     } }  function update(ajax) {       if (ajax.readystate==4 && ajax.status==200){           dataobj = eval('(' + ajax.responsetext + ')');           document.getelementbyid("status").innerhtml = dataobj.status;           document.getelementbyid("framenumber").innerhtml =           "frame:" + dataobj.firstframe + "/" + dataobj.lastframe;           document.getelementbyid("thumbnail").src = dataobj.imgsrc;       }       if (ajax.status==404) {           document.getelementbyid("status").innerhtml = "ajax updater not found";           document.getelementbyid("loading").src = "images/redx.png";       } } 

trying call open , send on ajax, throws errors if request has not finished within 2 seconds between each call setinterval. need check in each call whether ajax object has been sent or ready opening (check ajax.readystate).

in chrome, line

if (ajax.status==404) { 

causes error accessing status before ajax.readystate headers_received (2), loading (3), done (4). try making it

if (ajax.readystate == 4 && ajax.status==404) { 

to make sure object ready before accessing status.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -