Questions And Answers

More Tutorials

Javascript Global error handling in browsers

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.

Handling window.onerror to report all errors back to the server-side

The following example listens to window.onerror event and uses an image beacon technique to send the information through the GET parameters of an URL.

var hasLoggedOnce = false;
// Some browsers (at least Firefox) don't report line and column numbers
// when event is handled through window.addEventListener('error', fn). That's why
// a more reliable approach is to set an event listener via direct assignment.
window.onerror = function (eventOrMessage, url, lineNumber, colNumber, error) {
 if (hasLoggedOnce || !eventOrMessage) {
 // It does not make sense to report an error if:
 // 1. another one has already been reported -- the page has an invalid state and may produce
way too many errors.
 // 2. the provided information does not make sense (!eventOrMessage -- the browser didn't
supply information for some reason.)
 hasLoggedOnce = true;
 if (typeof eventOrMessage !== 'string') {
 error = eventOrMessage.error;
 url = eventOrMessage.filename || eventOrMessage.fileName;
 lineNumber = eventOrMessage.lineno || eventOrMessage.lineNumber;
 colNumber = eventOrMessage.colno || eventOrMessage.columnNumber;
 eventOrMessage = eventOrMessage.message || || error.message ||;
 if (error && error.stack) {
 eventOrMessage = [eventOrMessage, '; Stack: ', error.stack, '.'].join('');
 var jsFile = (/[^/]+\.js/i.exec(url || '') || [])[0] || 'inlineScriptOrDynamicEvalCode',
 stack = [eventOrMessage, ' Occurred in ', jsFile, ':', lineNumber || '?', ':', colNumber ||
 // shortening the message a bit so that it is more likely to fit into browser's URL length limit
(which is 2,083 in some browsers)
 stack = stack.replace(/https?\:\/\/[^/]+/gi, '');
 // calling the server-side handler which should probably register the error in a database or a
log file
new Image().src = '/exampleErrorReporting?stack=' + encodeURIComponent(stack);

 // window.DEBUG_ENVIRONMENT a configurable property that may be set to true somewhere else for
debugging and testing purposes.
 if (window.DEBUG_ENVIRONMENT) {
 alert('Client-side script failed: ' + stack);


In this page (written and validated by ) you learned about Javascript Global error handling in browsers . What's Next? If you are interested in completing Javascript tutorial, we encourage you simply to start here: Javascript Tutorial.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.