0

This is similar to How to tell if a <script> tag failed to load. But my question is specifically about plain old <script> tags in my HTML body like so

<script type="text/javascript" src="/path/to/my/script.js"></script>

I have a custom window.onerror function that I wrote to deal with general script errors. And indeed, if such a script has a syntax error or some other runtime error occurs while evaluating the script itself, that is handled correctly by my onerror function.

But if the script fails to load at all, because of an interrupted connection or even a 404 or other server error, window.onerror is not called. How can I detect this kind of failure programmatically in Javascript?

To clarify: I realize I could just put an onerror attribute in every single script tag. I'm asking if there's a better way, something comparable to my global window.onerror or some way of making that function be called.

6
  • 1
    I think this is exact duplicate of a post you linked. No matter how many times I read this. Is it just me? Commented Jul 24, 2014 at 14:25
  • That's why I mentioned "static" in my question and actually wrote out the script tag - this is not a dynamic script, it's just a plain old script tag in my HTML.
    – Dan
    Commented Jul 24, 2014 at 14:39
  • But I guess I see your point - it sounds like I need to add an onerror attribute to my script tag. Is that really the only way?
    – Dan
    Commented Jul 24, 2014 at 14:50
  • Where – in your opinion – is the difference between a script tag that was already inserted in the template and a script tag that was inserted later using JS? I don't see what is so special about your question in contrast to the one you've linked to.
    – feeela
    Commented Jul 24, 2014 at 14:54
  • Well, for example, for dynamic scripts I can use Ajax (in my case, wrapped with jQuery) and I will have an XHR object with a readyState and status that I can examine.
    – Dan
    Commented Jul 24, 2014 at 15:03

1 Answer 1

0

You can check youruniquefunction in your script tag

 if (typeof youruniquefunction!== 'undefined' ) {
   // your script ok
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.