遇到错误时防止Javascript停止 [英] Prevent Javascript from Stopping when Error is Encountered

查看:71
本文介绍了遇到错误时防止Javascript停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的产品将脚本插入到客户的网站中,就像一个实时聊天框.

Our product inserts a script into client's websites, kind of like a live chat box.

通常,客户的网站上有错误的JavaScript,这也会停止我们的代码(遇到错误时,浏览器会停止执行).即使控制台中有未定义的方法或变量之类的错误,有什么方法可以使我们的代码仍然执行?

Often, clients' websites have buggy javascript that also stops our code (the browser stops execution when errors are encountered). Is there any way to make our code still execute even though there are errors in the console about things like undefined methods or variables?

感谢您的帮助.

推荐答案

正如其他人所说,在发生错误后继续操作可能不是最好的选择,但是您可以尝试以下操作:

As others have said, continuing after an error might not be the best thing to do but you can try this:

function ignoreerror()
{
   return true
}
window.onerror=ignoreerror();

更多详细信息此处

每当JavaScript错误发生时,就会触发onerror事件(具体取决于 在您的浏览器配置中,您可能会看到一个错误对话框. onerror事件被附加到窗口对象上,这是非常不寻常的 避难的地方,但有充分的理由.这样附上 因此它可以监视页面上的所有JavaScript错误,即使是 页面的部分.

The onerror event fires whenever an JavaScript error occurs (depending on your browser configuration, you may see an error dialog pop up). The onerror event is attached to the window object, a rather unusual place to take refuge in, but for good reason. It is attached this way so it can monitor all JavaScript errors on a page, even those in the section of the page.

Opera的页面包含更多详细信息

Opera has a page with more details

Browsers supporting window.onerror
Chrome 13+
Firefox 6.0+
Internet Explorer 5.5+
Opera 11.60+
Safari 5.1+

这篇关于遇到错误时防止Javascript停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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