Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onerror does not work

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();
}
like image 861
Josh Stodola Avatar asked Dec 16 '09 16:12

Josh Stodola


People also ask

How do you trigger Onerror?

The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image). Tip: When used on audio/video media, related events that occurs when there is some kind of disturbance to the media loading process, are: onabort.

What is window Onerror?

onerror is a special browser event that fires whenever an uncaught JavaScript error has been thrown. It's one of the easiest ways to log client-side errors and report them to your servers. It's also one of the major mechanisms by which Sentry's client JavaScript integration (raven-js) works.

How do you use Onerror?

To use the onerror event, you must create a function to handle the errors. Then you call the function with the onerror event handler. The event handler is called with three arguments: msg (error message), url (the url of the page that caused the error) and line (the line where the error occurred).

What are the three information provided by Onerror () method?

What are the three information provided by Onerror () method? The onerror() method These are: Error message – It is the same message that the browser would display for the given error. URL – It is the file location where the error occurred. Line number – It is the line number in the given URL that caused the error.


2 Answers

"A common 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.com/en-us/library/ms976144.aspx

like image 148
MikeEL Avatar answered Oct 09 '22 09:10

MikeEL


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.

like image 23
Scott Marcus Avatar answered Oct 09 '22 11:10

Scott Marcus