Dr. Lawlor's Code, Robots, & Things

April 29, 2013

Chrome JavaScript InvalidStateError: DOM Exception 11

Filed under: Programming — Dr. Lawlor @ 2:35 pm

There’s an annoyingly cryptic error message in Chrome when your JavaScript code does an XMLHttpRequest incorrectly–“Uncaught Error: InvalidStateError: DOM Exception 11”. ¬†This error shows up when you access the request’s “status” inside onreadystatechange prior to hitting readyState 4. ¬†Firefox doesn’t complain about this, and Chrome only seems to care if you’re doing it inside onreadystatechange.

For example, this works fine:

var req=new XMLHttpRequest();
req.onreadystatechange=function()
{
  if (req.readyState==4 && req.status==200)
  {
    document.getElementById("div1").innerHTML=req.responseText;
  }
};
req.open("GET","/",true);
req.send();

But interchanging the order of the req.readyState and req.status checks results in “Uncaught Error: InvalidStateError: DOM Exception 11”.

FYI!

Advertisements

Create a free website or blog at WordPress.com.