最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.onerror does not work - Stack Overflow

programmeradmin3浏览0评论

I have some tricky AJAX code on a form, and sometimes it will fail (don't ask why, I can't get around it). When this happens, I need to trap the error, reset a hidden field indicator, and submit the form naturally so that the user does not have an unpleasant experience. I planned on using window.onerror to do this, but it is never firing! I am using IE8 and all I have to worry about is the IE browser. Is there some gotcha to getting this event to work? Here's my code...

window.onerror = function() {
  alert("Error!");
  document.getElementById("hidAjax").value = "0";
  document.forms[0].submit();
}

I have some tricky AJAX code on a form, and sometimes it will fail (don't ask why, I can't get around it). When this happens, I need to trap the error, reset a hidden field indicator, and submit the form naturally so that the user does not have an unpleasant experience. I planned on using window.onerror to do this, but it is never firing! I am using IE8 and all I have to worry about is the IE browser. Is there some gotcha to getting this event to work? Here's my code...

window.onerror = function() {
  alert("Error!");
  document.getElementById("hidAjax").value = "0";
  document.forms[0].submit();
}
Share Improve this question asked Dec 16, 2009 at 16:23 Josh StodolaJosh Stodola 82.5k48 gold badges186 silver badges228 bronze badges 1
  • 1 I would expect throw "test"; to trigger this handler. It does not. – Josh Stodola Commented Dec 16, 2009 at 16:36
Add a ment  | 

3 Answers 3

Reset to default 12

"A mon problem that bites many developers occurs when their onerror handler is not called because they have script debugging enabled for Internet Explorer. This will be the case by default if you have installed the Microsoft Script Debugger or Microsoft Visual Studio 6.0® (specifically Visual InterDev 6.0™)—onerror handling is how these products launch their debugger. You can disable script debugging for a given instance of Internet Explorer on the Advanced tab of the Internet Options dialog box (note that checking the Disable script debugging setting will apply only to that instance of Internet Explorer):"

http://msdn.microsoft./en-us/library/ms976144.aspx

try/catch also introduces an additional error object that only has the scope of the catch. In applications where performance matters, this is not a good idea.

Any reason not to just put a try/catch around the tricky code?

发布评论

评论列表(0)

  1. 暂无评论