我知道如果我们不想调用浏览器的 javascript 错误处理程序,window.onerror 应该返回的内容之间存在一些不一致。
由于我们的网站相当重 ajax,因此我们需要确保浏览器错误处理程序永远不会被调用。
我的意思是:
window.onerror=function(){
console.log('error captured');
return false;
}
显然,“return false”部分因浏览器而异。
有谁知道我们可以在某个地方找到一个列表,说明如何处理浏览器到浏览器,类似 quirksmode/caniuse 的东西?
或者如果列表相当小,有谁知道需要返回哪些值来抑制每个浏览器错误处理程序?
谢谢!
最佳答案
您的工作已经安排妥当。
首先,您从 onerror
处理程序返回 true
,而不是 false
...它应该停止。
接下来,有些浏览器的某些版本在这方面表现不佳。他们有时只是不像宣传的那样工作,似乎没有人知道为什么。我见过的最聪明的答案是关于调用堆栈大小变得太大……我觉得这听起来不错!
也就是说,这应该可以解决问题:
<script>
function stopErrors() {
return false;
}
window.onerror = stopErrors;
</script>
来自http://api.jquery.com/error/的一些要点:
Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers. Use window.onerror instead.
和
The event handler must be attached before the browser fires the error event ... Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and will generally not be triggered if the URL uses the file: protocol.
关于javascript - 浏览器之间不一致的 window.onerror 返回 true/false - 任何可用的正确返回值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15523071/