javascript 遇到错误时防止Javascript停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10825992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent Javascript from Stopping when Error is Encountered
提问by xd44
Our product inserts a script into client's websites, kind of like a live chat box.
我们的产品将脚本插入客户的网站,有点像实时聊天框。
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?
通常,客户的网站有错误的 javascript 也会停止我们的代码(当遇到错误时浏览器会停止执行)。即使控制台中存在有关未定义方法或变量之类的错误,是否有任何方法可以使我们的代码仍然执行?
Thanks for your help.
谢谢你的帮助。
采纳答案by Jason Kulatunga
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();
More details here
更多细节在这里
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.
每当发生 JavaScript 错误时都会触发 onerror 事件(根据您的浏览器配置,您可能会看到一个错误对话框弹出)。onerror 事件附加到 window 对象,这是一个相当不寻常的避难所,但有充分的理由。它以这种方式附加,因此它可以监视页面上的所有 JavaScript 错误,甚至是页面部分中的错误。
Opera has a page with more details
Opera 有一个包含更多详细信息的页面
Browsers supporting window.onerror
Chrome 13+
Firefox 6.0+
Internet Explorer 5.5+
Opera 11.60+
Safari 5.1+
回答by Chris Tonkinson
The short answer is that you really can't.
简短的回答是你真的不能。
"Solution" #1:You could insist that YOUR 3rd party code run before anyone else's. In most cases, this isn't possible or even desirable.
“解决方案”#1:您可以坚持让您的第 3 方代码在其他任何人之前运行。在大多数情况下,这是不可能的,甚至是不可取的。
"Solution" #2:You could insist that the 1st party engineers wrap all 3rd party code in try/catch
blocks. But, this solution really doesn't buy you any guarantee, because very frequently 3rd party libraries attach additional <script>
tags to the page - these would not fall under the "jurisdiction" of the try/catch
scope enclosing the code which created this/these tag(s).
“解决方案”#2:您可以坚持要求第一方工程师将所有第三方代码包装在try/catch
块中。但是,这个解决方案真的不能给你任何保证,因为非常频繁的 3rd 方库<script>
会在页面上附加额外的标签 - 这些不会属于try/catch
包含创建这个/这些标签的代码的范围的“管辖权” .
"Solution" #3:You could build YOUR app entirely within the scope of an <iframe>
, thereby avoiding the issue entirely. Unfortunately, even if you're very smart, you'll quickly run into cross domain violations, 3rd party cookie restrictions, and the like. It's very probable that this will not work for you.
“解决方案”#3:您可以完全在 范围内构建您的应用程序<iframe>
,从而完全避免该问题。不幸的是,即使您非常聪明,也会很快遇到跨域违规、第三方 cookie 限制等问题。这很可能对您不起作用。
"Solution" #4:You could explain the issue to your client, and have them demand that the other 3rd party code run cleanly. I say this is a "solution" because, frankly, it's not a "solution" to your question if your question is how to avoid doing exactly this.
“解决方案”#4:你可以向你的客户解释这个问题,并让他们要求另一个 3rd 方代码干净地运行。我说这是一个“解决方案”,因为坦率地说,如果您的问题是如何避免这样做,那么这不是您问题的“解决方案” 。
Unfortunately, option #4 is your best bet. It may help if you observe other 3rd party libraries "breaking" in the same fashion: you can tell your client "hey, it's not just me - X, Y, and Z are all also 'broken' because of <name of other 3rd party library>." It may cause them to put the heat on the offending code, which makes the web a happier place for all involved.
不幸的是,选项#4 是您最好的选择。如果您观察到其他 3rd 方库以同样的方式“损坏”,这可能会有所帮助:您可以告诉您的客户“嘿,不仅仅是我 - X、Y 和 Z 也都因为 <其他 3rd 的名称”而“损坏”党库>。” 这可能会导致他们对有问题的代码产生兴趣,这使网络成为所有相关人员更快乐的地方。
回答by Alex
Well, to me that work fine:
好吧,对我来说,工作正常:
element = document.querySelector('.that-pretty-element');
if (element != null) {
element.onclick = function () {
alert(" I'm working beibi ;) ");
}
}
querySelector()
returns false
, so, we can verify with if's
querySelector()
返回false
,因此,我们可以验证if's
回答by ahren
You can't from your code - they need to use try
/catch
for questionable pieces of script.
你不能从你的代码中提取 - 他们需要使用try
/catch
来处理有问题的脚本。
回答by Shaz
You could have them insert an iframe
into their page instead of you trying to inject code using a script
tag like so: http://jsfiddle.net/EzMGD/Notice how the script throws an error yet we can still see the content in the iframe. The iframe should help from using each others variables if applicable.
你可以让他们iframe
在他们的页面中插入一个,而不是你尝试使用script
像这样的标签来注入代码:http: //jsfiddle.net/EzMGD/注意脚本是如何抛出错误的,但我们仍然可以看到 iframe 中的内容。如果适用,iframe 应该有助于使用彼此的变量。
<script>
MeaningOfLife();
</script>
<iframe src="http://bing.com"></iframe>?
Or inject the code so it's the very first or very last script
.
或者注入代码,使其成为第一个或最后一个script
。