我想添加一個onerror事件到我的網站。window.onerror不工作在鉻
window.onerror = function() {
alert("an error");
}
但所有我得到的是:
notThere();
ReferenceError: notThere is not defined
我缺少什麼?
瀏覽器:鉻26.0.1410.64米
重現步驟:
- 將代碼添加到控制檯。
- 添加notthere的()到控制檯
我想添加一個onerror事件到我的網站。window.onerror不工作在鉻
window.onerror = function() {
alert("an error");
}
但所有我得到的是:
notThere();
ReferenceError: notThere is not defined
我缺少什麼?
瀏覽器:鉻26.0.1410.64米
重現步驟:
的window.onerror
作品在Chrome中(見的jsfiddle - http://jsfiddle.net/PWSDF/),但顯然不是在控制檯 - 這有一定的道理。
當控制檯直接產生錯誤時,window.onerror不會被觸發。它可以可以通過setTimeout
雖然觸發,例如,setTimeout(function() { notThere(); }, 0);
可能重複:Chrome: Will an error in code invoked from the dev console trigger window.onerror?
其他一些原因,你可能不能夠(自提那些開)處理錯誤window.onerror
:
window.onerror
。如果您使用的是Angular,則錯誤不會通過window.onerror
。您可以使用此工廠來處理他們:
.factory('$exceptionHandler', function() {
return function errorCatcherHandler(exception, cause) {
console.error(exception);
if (window.OnClientError) window.OnClientError(exception);
};
})
參見:
https://docs.angularjs.org/api/ng/service/ $ exceptionHandler的
http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
又有什麼意義? – 2016-07-12 10:28:28
我想,它會劫持錯誤,而不是在控制檯中顯示它們。 – Lachezar 2016-07-20 14:07:31