DelphiFAQ Home Search:
General :: Web publishing :: JavaScript
Code snippets to add functionality to your HTML pages, preferably cross-browser.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Catch javascript error messages before they pop up

Since version 1.1 client side Javascript has an event driven error handling model where you define a handler function for window.onerror to overwrite the error handling. The error handler has three arguments error message, url and line number. It should return true if the error is handled and return false to let the browser handle the error.

The sample code below shows an easy-catch-all handler and an advanced one which actually looks into the passed error information and tries to do something reasonable. The advanced version also restores the browser's default handler when done.

 // suppress all error messages and do nothing with them:
 function noErrorMessages () { return true; }
 window.onerror = noErrorMessages;
 // advanced: a full error handler
 function handleError (err, url, line) {
     if (err.indexOf('is not defined') != -1) {
       alert('Oops, something is not defined.\\n' +
              err + '\n' + url + '\nline no: ' + line);
       return true; // error is handled
       return false; // let the browser handle the error
 window.defaultOnError = window.onerror; // store default handler
 window.onerror = handleError; // assign own handler
 //.. executing my faulty code
 window.onerror = window.defaultOnError;  // restore default handler
Generated 8:01:42 on Jul 10, 2020