忽略页面中的 javascript 语法错误并继续执行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12219154/
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
Ignore javascript syntax errors in a page and continue executing the script
提问by Aakash Chakravarthy
I develop plugins for WordPress. It uses some jquery in the user side (themes) as a jquery plugin. The problem is, when there is an javascript error with other plugins made by other autors, my plugin's javascript fails to execute.
我为 WordPress 开发插件。它在用户端(主题)使用一些 jquery 作为 jquery 插件。问题是,当其他作者制作的其他插件出现 javascript 错误时,我的插件的 javascript 无法执行。
And the worst thing is people consider that there is a serious fault with my plugin even though it works 100% fine with error handling conditional statements. But it is actually due to some other javascript syntax errors of some other WP plugin/theme authors.
最糟糕的是人们认为我的插件存在严重错误,即使它在错误处理条件语句中 100% 正常工作。但这实际上是由于其他一些 WP 插件/主题作者的其他一些 javascript 语法错误。
Is there a way to continue execute my plugin JS ignoring other JS errors. Or can i have suggestions to handle this problem ??
有没有办法继续执行我的插件 JS 而忽略其他 JS 错误。或者我可以有处理这个问题的建议吗??
回答by Cosmin
Try :
尝试 :
window.onerror = function(){
return true;
}
That is if you can include this before the bugged script has been executed.
也就是说,如果您可以在执行错误脚本之前包含它。
回答by Krishna Kumar
You should correct the old JavaScript error because it may create many problems, not for right now but for next time.
您应该更正旧的 JavaScript 错误,因为它可能会产生许多问题,不是现在,而是下次。
Put your JavaScript file / code at the top (before JS having error), and call it before JavaScript effected by other JavaScript code.
将您的 JavaScript 文件/代码放在顶部(在 JS 出错之前),并在其他 JavaScript 代码影响 JavaScript 之前调用它。
In case you need handle JavaScript exception at run time, best option is
如果您需要在运行时处理 JavaScript 异常,最好的选择是
try { /* run js code */ }
catch (error){ /* resolve the issue or bug */ }
回答by Shmiddty
You should be able to swallow any error using the error
event:
您应该能够使用error
事件吞下任何错误:
$(window).error(function(e){
e.preventDefault();
});
I've never attempted this, but it should work in theory.
我从来没有尝试过这个,但理论上它应该有效。
It should be noted that this probably isn't a great solution to your problem. It could be that your plugin is interfering with other plugins. It is, of course, possible that the errors are no fault of your own, but generally speaking (with publicly released plugins) not the case.
应该注意的是,这可能不是解决您问题的好方法。可能是您的插件干扰了其他插件。当然,错误可能不是您自己的错,但一般来说(使用公开发布的插件)并非如此。
回答by Jhong
I encountered the same problem: There are a bunch of plugins out there that make too many assumptions and cause JavaScript errors on YOUR page, even when you are being a good citizen; these errors have nothing to do with your plugin.
我遇到了同样的问题:即使你是一个好公民,也有很多插件会做出太多假设并导致页面上出现 JavaScript 错误;这些错误与您的插件无关。
There's no sense trying to handle errors in other plugins -- IMO it is better to be self-contained but resilient to their errors.
尝试处理其他插件中的错误是没有意义的——IMO 最好是自包含但对它们的错误有弹性。
In my case, the errors were halting the jquery DOM ready event, and my JavaScript init code wasn't getting executed. The exact form of the error isn't important though -- the solution is just to fire on multiple events.
就我而言,错误导致 jquery DOM 就绪事件停止,并且我的 JavaScript 初始化代码没有得到执行。错误的确切形式并不重要——解决方案只是触发多个事件。
The solution for me was to have fallbacks in addition to relying on the jQuery DOM ready event:
我的解决方案是除了依赖 jQuery DOM ready 事件之外还有回退:
- I wrapped any code I wanted to fire on the DOM ready event into their own function -- e.g. my_hardened_init_action();
- I added a function, my_hardened_init(), that only runs once. It calls my_hardened_init_action() the first time it is called, and does nothing on subsequent calls.
- I added various methods to call my_hardened_init() in the WordPress footer. In my case, I only needed two. First, trying the usual jQuery DOM init, but falling back to a simple setTimeout(). So if the jQuery DOM init never fires due to broken JavaScript, the timeout will fire shortly after when the page has finished loading.
- 我将我想在 DOM 就绪事件上触发的任何代码包装到它们自己的函数中——例如 my_hardened_init_action();
- 我添加了一个仅运行一次的函数 my_hardened_init()。它在第一次调用时调用 my_hardened_init_action(),并且在后续调用中不执行任何操作。
- 我在 WordPress 页脚中添加了各种方法来调用 my_hardened_init()。就我而言,我只需要两个。首先,尝试通常的 jQuery DOM init,但又回到简单的 setTimeout()。因此,如果 jQuery DOM init 从未因 JavaScript 损坏而触发,则超时将在页面加载完成后不久触发。
You could add multiple other fallbacks -- even add the code to the header if needs be. As my_hardened_init() only runs once, you can try as many times as you like to trigger it.
您可以添加多个其他回退——如果需要,甚至可以将代码添加到标题中。由于 my_hardened_init() 只运行一次,您可以尝试多次触发它。
This has worked on a bunch of client sites with a range of other broken plugins.
这已经在一堆客户端站点上使用了一系列其他损坏的插件。
Hope this helps.
希望这可以帮助。
回答by ScottE
What about window.onerror
?
怎么样window.onerror
?
回答by Rogerio de Moraes
If you page is running. Use:
如果您的页面正在运行。用:
$(window).error(function(e){
e.preventDefault();
}); // IGNORE ALL ERROR JQUERY!
or use:
或使用:
window.onerror = function(){
return true;
} // IGNORE ALL ERROR JAVASCRIPT!
回答by Rogerio de Moraes
<script>
var _z = console;
Object.defineProperty(window, 'console', {
get: function(){
if (_z._commandLineAPI) {
return true;
}
return _z;
},
set: function(val) {
_z = val;
}
});
</script>
回答by Enginerd Sunio
This is worked for me:
这对我有用:
try{
//your code
}
catch(error)
{
return true;
}
回答by codelove
A regular try catch statements won't work for these types of errors.
常规的 try catch 语句不适用于这些类型的错误。
Possible workarounds:
可能的解决方法:
Use inline-css to float the bar to the left instead of jQuery (I am not sure what the full function of your plugin is but if it just floats the bar to the left why not just use css?)
使用 inline-css 将栏浮动到左侧而不是 jQuery(我不确定您插件的完整功能是什么,但如果它只是将栏浮动到左侧,为什么不只使用 css?)
Have a invisible iframe which tries to run some code on the parent page. if it fails than alert the user(from within this iframe) that it wasn't your fault :)
有一个不可见的 iframe,它试图在父页面上运行一些代码。如果它失败,则警告用户(在此 iframe 中)这不是您的错:)
回答by Ian Wizard
Perhaps you could try putting your js in an html object, so that it's executed in a separate page. That probably won't help much if the js on the main page wont run to interact with the object. Just something to play with.
也许您可以尝试将 js 放在 html 对象中,以便在单独的页面中执行。如果主页上的 js 无法运行以与对象交互,那可能不会有太大帮助。只是玩玩的东西。