Can `window.onerror` be used to write the error stack trace as a global error handler

I am trying to make a global error handler for any javascript error in the browser, so I used this code in the tag <head>

:

window.onerror = function(msg, url, line, col, error) {
    // Note that col & error are new to the HTML 5 spec and may not be
    // supported in every browser.  It worked for me in Chrome.
    var extra = !col ? '' : '\ncolumn: ' + col;
    extra += !error ? '' : '\nerror: ' + error;

    // You can view the information in an alert to see things working like this:
    alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);

    // TODO: Report this error via ajax so you can keep track
    //       of what pages have JS issues

    var suppressErrorAlert = true;
    // If you return true, then error alerts (like in older versions of
    // Internet Explorer) will be suppressed.
    return suppressErrorAlert;
};

      

But since I'm using a CDN with minifying my javascript files, I always get these useless values โ€‹โ€‹passed to the function:

msg: Script error.

url: ''

string: 0

col: 0

error: null

(results from Chrome version 43.0.2357.125 (64-bit)

on Ubuntu)

So, is there anyway I can get the error stack trace (or even any information about it) from within window.onerror

?

+3


source to share





All Articles