Stats

61 Contributors: 1 Friday, July 22, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Global error handling in browsers

Syntax

  • window.onerror = function (eventOrMessage, url, lineNumber, colNumber, error) { ... }

Parameters

ParameterDetails
eventOrMessageSome browsers will call the event handler with just one argument, an Event object. However, other browsers, especially the older ones and older mobile ones will supply a String message as a first argument.
urlIf a handler is called with more than 1 argument, the second argument usually is an URL of a JavaScript file that is the source of the problem.
lineNumberIf a handler is called with more than 1 argument, the third argument is a line number inside the JavaScript source file.
colNumberIf a handler is called with more than 1 argument, the fourth argument is the column number inside the JavaScript source file.
errorIf a handler is called with more than 1 argument, the fifth argument is sometimes an Error object describing the problem.

Remarks

Unfortunately, window.onerror has historically been implemented differently by each vendor. The information provided in the Parameters section is an approximation of what to expect across different browsers and their versions.

Related Examples