5

The onerror page on MSDN states that the onerror handler can be attached to a script element and that it "Fires when an error occurs during object loading.".

For the purpose of unit tests, I am trying to get this onerror handler to fire, but could not find a suitable example.

The following code triggers an error in Firefox, but no alert is displayed in Internet Explorer

<script src="http://www.google.com/NOTFOUND.js" onerror="alert('error fired')"></script>

Does anyone know a value for script.src that would fire the handler attached to script.onerror?

Eric Bréchemier
  • 1,908
  • 3
  • 18
  • 30
  • IIRC, something like `src="1"` should trigger error. – kangax Jan 09 '10 at 03:16
  • @kangax: nice try but no, src="1" only triggers window.onerror, not script.onerror – Eric Bréchemier Jan 09 '10 at 13:47
  • not really related to this question but I noticed you committed to the code review proposal. Your rep score isn't showing up correctly for some reason. You might need to link your account or confirm some email to fix it. Lower rep has less impact on the proposal commit unfortunately. – greatwolf Jan 13 '11 at 13:49
  • @Victor T. Thanks for noticing, I just tried to commit once again. Let's see. – Eric Bréchemier Jan 18 '11 at 17:03

3 Answers3

9

I found this buried in some MSDN documentation:

Note that the documentation mistakenly says this works for elements too; the error will be fixed in the Workshop documentation for the final release of Internet Explorer 5 in March.

The next thing I thought of that could help is the onreadystatechange event:

<script src="http://www.google.com/NOTFOUND.js" onreadystatechange="alert(this.readyState)">

This event fires twice for me, once with "loading" and again with "loaded", whether the script is valid or not. Other documentation I've found says that sometimes it fires a complete event, and it's not really clear when it's supposed to fire. So it looks like that won't work.

So I think you're left with the hacky solution of checking that a variable which the script is supposed to declare really exists. In HTML:

<script src="http://yourdomain.com/declare_foo.js"></script>
<script>if (typeof foo == "undefined") {alert ('error loading script');}</script>

And then of course in declare_foo.js, you'd have

var foo = 'Script loaded successfully';
Dan F
  • 11,958
  • 3
  • 48
  • 72
Annie
  • 6,621
  • 22
  • 27
  • 1
    Very nice catch!! Sadly I cannot assume that a particular variable will be present in the script, as this code will be used in the wild to load third party scripts dynamically. I added a comment to the onerror page on MSDN to point back to the article and the note you found. Thank you Annie. – Eric Bréchemier Jan 09 '10 at 14:16
  • @Tom nope, no workaround. Some errors just cannot be caught in JavaScript. – Eric Bréchemier Nov 16 '10 at 13:08
  • The only way I could address this (super-bad workaround) is to check whether loaded has been fired within 15 seconds! – Shamasis Bhattacharya May 09 '12 at 08:00
0

Having just done a bit of reading on this, it looks like onerror can also be attached to the window object. See:

http://www.javascriptkit.com/javatutors/error.shtml

According to that page, you can pass in msg, url and linenumber arguments:

window.onerror=function(msg, url, linenumber){
 alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
 return true
}

Not sure on the browser support for this, but thanks for bringing it to my attention!

Update: didn't do anything in Safari 4 (apart from logging an error in firebug as per usual), but shows the alert in Firefox 3.6 Beta 5. Error test at http://www.javascriptkit.com/javatutors/errortest2.htm

Update 2: done a test with the following:

index.html:

<html>
<head>
<script type="text/javascript">

window.onerror=function(msg, url, linenumber){
 alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
 return true
}
</script>

<script type="text/javascript" src="js.js"></script>
</head>
<body>
</body>
</html>

js.js: (stupid name, i know!)

document.write('hi there'

Result in FF 3.6 Beta 5:

Result

Community
  • 1
  • 1
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • 1
    Thanks for the comment. I should have mentioned that I know how to trigger the window.onerror handler; here I am specifically interested in the script.onerror handler. – Eric Bréchemier Jan 08 '10 at 13:44
  • 2
    Thanks Adam, but I am really trying to fire script.onerror here, not window.onerror. I want to detect the (unsuccessful) end of loading a dynamic script, and setting the handler to window.onerror would not really help. – Eric Bréchemier Jan 08 '10 at 14:00
  • hello @Adam-Hopkinson! Could you please update your answer? The image is broken, cause the twitpic.com service is dead. – naXa stands with Ukraine Aug 15 '23 at 19:37
0

How about this?

<script language="JavaScript" type="text/javascript">
     var DidItLoad=0;
     function funcDidItLoad()
     { 
         if(DidItLoad==1)
         {
            alert("good")
         }
         else
         {
            alert("bad")
         }
         DidItLoad=0;
         return 0;
     }
     </script>
     <script language="JavaScript" type="text/javascript" onload="DidItLoad=1;" src="javascript/myError.js"></script><!--erase me--> 
     <script language="JavaScript" type="text/javascript">
        funcDidItLoad();
     </script>
Tim
  • 1