javascript:如何在弹出式警报中显示脚本错误? [英] javascript: how to display script errors in a popup alert?

查看:99
本文介绍了javascript:如何在弹出式警报中显示脚本错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  window.onerror = function(){
var message = / *获取错误消息并将其放在这里* /;
alert(message);
返回true;
};


解决方案

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

请参阅这里:



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

这篇关于javascript:如何在弹出式警报中显示脚本错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆