Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following is tested and working in IE 6 and Firefox 3.0.11:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Title&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; window.onerror = function (msg, url, num) { alert(msg + ';' + url + ';' + num); return true; } &lt;/script&gt; &lt;div&gt; ...content... &lt;/div&gt; &lt;script type="text/javascript"&gt; blah; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If some other JavaScript library you are loading is also attaching itself to <code>window.onerror</code> you can do this:</p> <pre><code>&lt;script type="text/javascript"&gt; function addHandler(obj, evnt, handler) { if (obj.addEventListener) { obj.addEventListener(evnt.replace(/^on/, ''), handler, false); // Note: attachEvent fires handlers in the reverse order they // were attached. This is the opposite of what addEventListener // and manual attachment do. //} else if (obj.attachEvent) { // obj.attachEvent(evnt, handler); } else { if (obj[evnt]) { var origHandler = obj[evnt]; obj[evnt] = function(evt) { origHandler(evt); handler(evt); } } else { obj[evnt] = function(evt) { handler(evt); } } } } addHandler(window, 'onerror', function (msg, url, num) { alert(msg + ';' + url + ';' + num); return true; }); addHandler(window, 'onerror', function (msg, url, num) { alert('and again ' + msg + ';' + url + ';' + num); return true; }); &lt;/script&gt; </code></pre> <p>The above lets you attach as many <code>onerror</code> handlers as you want. If there is already an existing custom <code>onerror</code> handler it will invoke that one, then yours.</p> <p>Note that <code>addHandler()</code> can be used to bind multiple handlers to any event:</p> <pre><code>addHandler(window, 'onload', function () { alert('one'); }); addHandler(window, 'onload', function () { alert('two'); }); addHandler(window, 'onload', function () { alert('three'); }); </code></pre> <p>This code is new and somewhat experimental. I'm not 100% sure <a href="https://developer.mozilla.org/en/DOM/element.addEventListener" rel="noreferrer">addEventListener</a> does precisely what the manual attachment does, and as commented, <a href="http://msdn.microsoft.com/en-us/library/ms536343.aspx" rel="noreferrer">attachEvent</a> fires the handlers in the reverse order they were attached in (so you would see 'three, two, one' in the example above). While not necessarily "wrong" or "incorrect", it is the opposite of what the other code in <code>addHandler</code> does and as a result, could result in inconsistent behaviour from browser to browser, which is why I removed it.</p> <p>EDIT:</p> <p>This is a full test case to demonstrate the onerror event:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Title&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; function addHandler(obj, evnt, handler) { if (obj.addEventListener) { obj.addEventListener(evnt.replace(/^on/, ''), handler, false); } else { if (obj[evnt]) { var origHandler = obj[evnt]; obj[evnt] = function(evt) { origHandler(evt); handler(evt); } } else { obj[evnt] = function(evt) { handler(evt); } } } } addHandler(window, 'onerror', function (msg, url, num) { alert(msg + ';' + url + ';' + num); return true; }); &lt;/script&gt; &lt;div&gt; ...content... &lt;/div&gt; &lt;script type="text/javascript"&gt; blah; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When the above code is put in test.htm and loaded into Internet Explorer from the local idks, you should see a dialog box that says <code>'blah' is undefined;undefined;undefined</code>.</p> <p>When the above code is put in test.htm and loaded into Firefox 3.0.11 (and the latest 3.5 as of this edit - Gecko/20090616) from the local disk, you should see a dialog box that says <code>[object Event];undefined;undefined</code>. If that is not happening then your copy of Firefox is not configured correctly or otherwise broken. All I can suggest is that you remove Firefox, remove your local profile(s) (information about how to find your profile is available <a href="http://support.mozilla.com/en-US/kb/Profiles" rel="noreferrer">here</a>) and reinstall the latest version and test again.</p>
 

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