window.onerror not working in chrome

Dinkheller picture Dinkheller · Apr 24, 2013 · Viewed 25.6k times · Source

I am trying to add an onerror event to my website.

window.onerror = function() {
    alert("an error");
}

But all I receive is:

notThere();
ReferenceError: notThere is not defined

What am I missing?

Browser: Chrome 26.0.1410.64 m

Steps to reproduce:

  • add the code to the console.
  • add notThere() to the console

Answer

wrschneider picture wrschneider · Dec 2, 2014

window.onerror is not triggered when the console directly generates an error. It can be triggered via setTimeout though, e.g., setTimeout(function() { notThere(); }, 0);

Possible duplicate: Chrome: Will an error in code invoked from the dev console trigger window.onerror?