Click here to Skip to main content
12,953,541 members (50,925 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 30 Nov 2011

Back to Basics – JavaScript onerror Event

, 30 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
The window.onerror is an event handler for error events that are sent to the window.

Back to Basics – JavaScript onerror Event

The JavaScript window.onerror event is a very useful event that I find very strange that sometimes client-side developers don’t know. The event is fired most of the times (yep, not every time) an error occurs in the JavaScript code. You can wire a handler to the event that will help you to prevent errors from bubbling to the browser or to send the errors to some logger for further investigation.

The onerror Event

The window.onerror is an event handler for error events that are sent to the window. It isn’t supported in all the browsers (mostly in old browsers) so you can’t take it for granted that it will work, but most of the time it will. It takes three optional parameters:

  • An error message 
  • A URL where the error was raised 
  • The line number of the line that raised the error

Here is a simple code sample which will suppress most of the raised JavaScript errors:

window.onerror = function(msg, url, lineNumber) {   
    return true; // prevents browser error messages  

While the previous code will stop the bubbling of the errors to the browser, I prefer to do more than just preventing the annoying browser error dialogs. You can add an AJAX call in order to send the error to a logger that is sitting on your site. This will help you to monitor and diagnose JavaScript errors and will help you to fix JavaScript bugs.


The onerror event handler can be very useful and might help prevent browser JavaScript error dialogs. Even so, you might be careful when using it since not all of the browsers support it. Combining it with a logging framework such as log4js, log4javascript or just plain AJAX functionality for logging might help you monitor the JavaScript errors in your web site/application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 30 Nov 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid