Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onerror not working in chrome

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
like image 739
Dinkheller Avatar asked Apr 24 '13 12:04

Dinkheller


People also ask

What triggers window Onerror?

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.

What is window Onerror?

Introduction. onerror is a DOM event handler. It is started when an error occurs during object loading. While window. onerror is an event handler, there is no error event being fired: instead, when there is an uncaught exception or compile-time error, the window.

What is the use of JavaScript Onerror event?

What is onerror() Method in JavaScript? The onerror event handler was the first feature to facilitate error handling in JavaScript. The error event is fired on the window object whenever an exception occurs on the page.


2 Answers

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?

like image 74
wrschneider Avatar answered Nov 10 '22 15:11

wrschneider


The window.onerror works in Chrome (see jsfiddle - http://jsfiddle.net/PWSDF/), but apparently not in the console - which makes some sense.

like image 31
Lachezar Avatar answered Nov 10 '22 13:11

Lachezar