javascript - IE - 防止在 IE 中显示错误

标签 javascript internet-explorer error-handling

我正在开发一个网络应用程序。我的应用程序在 chrome 和 firefox 上运行良好,但由于某种原因在 IE 中出现了一些错误。即使出现几个错误,应用程序仍然可以顺利运行,没有明显的问题。

我想对最终用户隐藏错误,因为目前他看到一个小图标,表示发生了错误。

我怎样才能做到这一点?

谢谢

最佳答案

到目前为止,最好的办法是找出代码导致错误的位置并修复它。

更新:以下内容适用于 IE8,但不适用于 IE9 或 IE11(因此可能不适用于 IE10):

因为这在 IE 中特别发生,您可以使用window.onerror如果它们是运行时(不是编译)错误,则处理(抑制)它们,从您对另一个答案的评论来看,这听起来像是。从该链接:

To suppress the default Internet Explorer error message for the window event, set the returnValue property of the event object to true or simply return true 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 returning true. 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);
};

Live copy

但是如果我们添加这个,错误就会被抑制,因为我们告诉 IE 我们已经处理了它:

window.onerror = function() {
  // Return true to tell IE we handled it
  return true;
};

Live copy

关于javascript - IE - 防止在 IE 中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5301383/

相关文章:

javascript - Canvas 应用程序在 IE9 中不工作,在 FF4b7 中工作不正常

selenium - 为什么即使使用FailureHandling.CONTINUE_ON_FAILURE也会停止测试用例

c# - 捕获HttpRequestValidationException MVC

html - 如何使用 CSS 在 IE 和 chrome 上隐藏 "image not found"图标

javascript - JavaScript 中的浏览器检测 (Internet Explorer)

c# - 如何在 Global.asax 中获取来自另一个 .aspx 页面的 log4net 日志记录信息?

javascript - 使用 jquery.inline-edit.js 内联编辑 Textarea – 获取 id 并保存

javascript - VueJS - 事件发出 - 在其他计算之前显示 HTML

javascript - 防止在网页上缓存图像

javascript - 是否可以使用 jQuery 的 .when 而不使用 ajax 调用?