遇到错误时防止Javascript停止

编程入门 行业动态 更新时间:2024-10-28 02:36:36
本文介绍了遇到错误时防止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停止

本文发布于:2023-10-16 00:40:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!