我正在开发一个网络应用程序。我的应用程序在 chrome 和 firefox 上运行良好,但由于某种原因在 IE 中出现了一些错误。即使出现几个错误,应用程序仍然可以顺利运行,没有明显的问题。
我想对最终用户隐藏错误,因为目前他看到一个小图标,表示发生了错误。
我怎样才能做到这一点?
谢谢
最佳答案
到目前为止,最好的办法是找出代码导致错误的位置并修复它。
更新:以下内容适用于 IE8,但不适用于 IE9 或 IE11(因此可能不适用于 IE10):
因为这在 IE 中特别发生,您可以使用window.onerror
如果它们是运行时(不是编译)错误,则处理(抑制)它们,从您对另一个答案的评论来看,这听起来像是。从该链接:
To suppress the default Internet Explorer error message for the
window
event, set thereturnValue
property of theevent
object totrue
or simply returntrue
in Microsoft JScript.The
onerror
event fires for run-time errors, but not for compilation errors. In addition, error dialog boxes raised by script debuggers are not suppressed by returningtrue
. To turn off script debuggers, disable script debugging in Internet Explorer by choosing Internet Options from the Tools menu. Click the Advanced tab and select the appropriate check box(es).
示例:
此代码会导致错误(因为我正在尝试取消引用 undefined
):
document.getElementById('theButton').onclick = function() {
var d;
display(d.foo);
};
但是如果我们添加这个,错误就会被抑制,因为我们告诉 IE 我们已经处理了它:
window.onerror = function() {
// Return true to tell IE we handled it
return true;
};
关于javascript - IE - 防止在 IE 中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5301383/