Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

javascript: how to display script errors in a popup alert?

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;
};
question from:https://stackoverflow.com/questions/2604976/javascript-how-to-display-script-errors-in-a-popup-alert

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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+'
URL: '+url+'
Line 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...