45

I want to display script errors in a popup alert instead of showing them in the browser console.

window.onerror = function() {
  var message = /* get error messages and put them here */;
  alert(message);
  return true;
};
DVK
  • 126,886
  • 32
  • 213
  • 327
powerboy
  • 10,523
  • 20
  • 63
  • 93
  • 3
    Note that the `onerror` event is only supported by IE and Mozilla. – Christian C. Salvadó Apr 09 '10 at 04:01
  • 1
    So is there a cross-browser solution? I mean, a similar function supported by webkit – powerboy Apr 09 '10 at 04:05
  • 1
    You mean there are browsers other than IE and Mozilla??? Dang! I need to get out more :) – DVK Apr 09 '10 at 04:10
  • 2
    Actually it'll be supported (hopefully soon) in WebKit - see http://www.phwinfo.com/forum/comp-lang-javascript/401318-window-onerror-chrome.html#post1867841 – DVK Apr 09 '10 at 04:12
  • yep! But I got another problem. Seems that onerror only support those 3 parameters. There is no way to catch an error of a specific name. – powerboy Apr 09 '10 at 04:30
  • `onerror` is useful for debugging. You should not be using it for production code. For catching errors the normal course of your scripts you should be using exceptions. – bobince Apr 09 '10 at 09:11

5 Answers5

65

Yes, that is the correct way.

See the reference here:

http://www.javascriptkit.com/javatutors/error2.shtml

And explanation of how to see more details of the error here:

http://www.javascriptkit.com/javatutors/error3.shtml

Their example:

window.onerror = function(msg, url, linenumber) {
    alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
}

If you wish to display a LIST of errors in a single pop-up, it's trickier.

Since the errors occue 1 by 1, you need to do the following:

  • have window.onerror handler store error details in some array
  • Check that array periodically - either via a timer, or on every N'th call of window.onerror handler, or both.

    When the check happens, process entire array, display contents as desired, and empty out an array

DVK
  • 126,886
  • 32
  • 213
  • 327
  • 3
    Thx! So the answer is: window.onerror = function(message) { alert(message); return true; }; Just tested! – powerboy Apr 09 '10 at 04:02
3

Just in case someone would like to use it with jQuery:

$(window).on("error", function(evt) {

    console.log("jQuery error event:", evt);
    var e = evt.originalEvent; // get the javascript event
    console.log("original event:", e);
    if (e.message) { 
        alert("Error:\n\t" + e.message + "\nLine:\n\t" + e.lineno + "\nFile:\n\t" + e.filename);
    } else {
        alert("Error:\n\t" + e.type + "\nElement:\n\t" + (e.srcElement || e.target));
    }
});
user1398498
  • 377
  • 3
  • 11
0

Have a look at The onerror event of the window object, specifically Getting additional details on an error

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
0

Check this out: http://www.javascriptkit.com/javatutors/error3.shtml. Looks like signature is function(message, url, linenumber).

Bialecki
  • 30,061
  • 36
  • 87
  • 109
-1
<script>$(window).on("error", function(evt) {

console.log("jQuery error event:", evt);
var e = evt.originalEvent; // get the javascript event
console.log("original event:", e);
if (e.message) { 
    alert("Error:\n\t" + e.message + "\nLine:\n\t" + e.lineno + "\nFile:\n\t" + e.filename);
} else {
    alert("Error:\n\t" + e.type + "\nElement:\n\t" + (e.srcElement || e.target));
}
});
</script>
Aurasphere
  • 3,841
  • 12
  • 44
  • 71