且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript:如何在弹出式警报中显示脚本错误?

更新时间:2023-02-19 11:05:13

是的,这是正确的方式。 >

请参阅这里:



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



以及如何查看更多内容的解释



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



他们的例子:

  window.onerror = function(msg,url,linenumber){
alert('错误消息:'+ msg +'\\\
URL:'+ url +'\\\
Line Number:'行号+);
返回true;
}

如果您希望在单个弹出窗口中显示错误列表,这很棘手。



由于错误是1乘1,您需要执行以下操作:




  • window.onerror 某些数组中的处理程序存储错误详细信息

  • 定期检查数组 - 通过一个计时器,或者每个N点调用 window.onerror 处理程序,或两者。



    当检查发生,处理整个数组,根据需要显示内容,并清空数组



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;
};

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