rad*_*byx 0 javascript error-handling warnings
如果我收到Javascript错误,是否可以通知visuel?
在开发中我有Firebug或其他东西打开所以我发现它.但是,如果我为别人做一个轻微的演示,我就不能打开它.
我仍然更喜欢知道错误,而不是默默地失败,我不知道跟踪错误,我无法区分真实错误和尾随错误.
您可以使用错误消息在try-catch和call alert中包围您的代码.例如,如果您的代码如下:
var x = document.getElementById("wrong_id"); //returns null
x.innerHTML = "Hello world!"; //null exception
Run Code Online (Sandbox Code Playgroud)
您可以使用try-catch将其包围,如下所示:
try {
var x = document.getElementById("wrong_id"); //returns null
x.innerHTML = "Hello world!"; //null exception
}
catch(err) {
alert(err.message);
}
Run Code Online (Sandbox Code Playgroud)
err.message 基本上包含错误消息,类似于您在Firebug中看到的错误消息.
编辑:您也可以定义window.onerror.看到这个答案
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |