Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>How to Catch Unhandled Javascript Errors</h1> <p>Assign the <code>window.onerror</code> event to an event handler like:</p> <pre><code>&lt;script type="text/javascript"&gt; window.onerror = function(msg, url, line, col, error) { // Note that col &amp; error are new to the HTML 5 spec and may not be // supported in every browser. It worked for me in Chrome. var extra = !col ? '' : '\ncolumn: ' + col; extra += !error ? '' : '\nerror: ' + error; // You can view the information in an alert to see things working like this: alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra); // TODO: Report this error via ajax so you can keep track // of what pages have JS issues var suppressErrorAlert = true; // If you return true, then error alerts (like in older versions of // Internet Explorer) will be suppressed. return suppressErrorAlert; }; &lt;/script&gt; </code></pre> <p>As commented in the code, if the return value of <code>window.onerror</code> is <code>true</code> then the browser should suppress showing an alert dialog.</p> <h2>When does the window.onerror Event Fire?</h2> <p>In a nutshell, the event is raised when either 1.) there is an uncaught exception or 2.) a compile time error occurs.</p> <blockquote> <p><strong>uncaught exceptions</strong></p> <ul> <li>throw "some messages"</li> <li>call_something_undefined();</li> <li>cross_origin_iframe.contentWindow.document;, a security exception</li> </ul> <p><strong>compile error</strong></p> <ul> <li><code>&lt;script&gt;{&lt;/script&gt;</code></li> <li><code>&lt;script&gt;for(;)&lt;/script&gt;</code></li> <li><code>&lt;script&gt;"oops&lt;/script&gt;</code></li> <li><code>setTimeout("{", 10);</code>, it will attempt to compile the first argument as a script</li> </ul> </blockquote> <h2>Browsers supporting window.onerror</h2> <ul> <li>Chrome 13+</li> <li>Firefox 6.0+</li> <li>Internet Explorer 5.5+</li> <li>Opera 11.60+</li> <li>Safari 5.1+</li> </ul> <h2>Screenshot:</h2> <p>Example of the onerror code above in action after adding this to a test page:</p> <pre><code>&lt;script type="text/javascript"&gt; call_something_undefined(); &lt;/script&gt; </code></pre> <p><img src="https://i.stack.imgur.com/cl91I.png" alt="Javascript alert showing error information detailed by the window.onerror event"></p> <h2>JSFiddle:</h2> <p><a href="https://jsfiddle.net/nzfvm44d/" rel="noreferrer">https://jsfiddle.net/nzfvm44d/</a></p> <h2>References:</h2> <ul> <li><a href="https://developer.mozilla.org/en/DOM/window.onerror" rel="noreferrer">Mozilla Developer Network :: window.onerror</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ms976144.aspx" rel="noreferrer">MSDN :: Handling and Avoiding Web Page Errors Part 2: Run-Time Errors</a></li> <li><a href="http://blogs.microsoft.co.il/blogs/gilf/archive/2011/11/23/back-to-basics-javascript-onerror-event.aspx" rel="noreferrer">Back to Basics – JavaScript onerror Event</a></li> <li><a href="http://dev.opera.com/articles/view/better-error-handling-with-window-onerror/" rel="noreferrer">DEV.OPERA :: Better error handling with window.onerror</a></li> <li><a href="http://www.mattsnider.com/window-onerror-event/" rel="noreferrer">Window onError Event</a></li> <li><a href="http://www.javascriptkit.com/javatutors/error2.shtml" rel="noreferrer">Using the onerror event to suppress JavaScript errors</a></li> <li><a href="https://stackoverflow.com/q/1008692/590956">SO :: window.onerror not firing in Firefox</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload