1

Consider the following:

var a_script_from_my_dom = document.querySelector("script")

How can I test, preferably without the use of third party libraries, if this script has finished loading and executing? Assume that the script may have been inserted dynamically using createElement / appendChild. Also assume that I have no power to change the loaded script to set specific variables or trigger specific behaviour.

I know that I can attach an onload handler, but if the script has already loaded, the handler won't run.

Niels B.
  • 5,912
  • 3
  • 24
  • 44
  • You'll have to find something that the script affects and test that element to see if it has been affected as it should have been. – Scott Marcus May 25 '17 at 21:52
  • First of all, an `alert` interrupts the user. That is in no way the point. My goal is runtime dependency management. Secondly, I cannot depend on particular side effects from the loaded script - the main use case are libraries from CDN's. I don't control these. – Niels B. May 25 '17 at 21:52
  • You said this was for testing, not production. That's why I suggested `alert()`. Additionally, you didn't elaborate until after I made that comment. – Scott Marcus May 25 '17 at 21:52
  • Sorry, maybe I was not clear. Test as in programatically testing. – Niels B. May 25 '17 at 21:53
  • There doesn't seem to be any property in a `script` element that holds the status of loading it. It looks like this just isn't possible. – Barmar May 25 '17 at 22:17
  • That's also my draft conclusion so far. I hope someone will prove me wrong. :-/ – Niels B. May 25 '17 at 22:28
  • What do text contents of script define and do? – guest271314 May 25 '17 at 22:31
  • If you know what the script does you can easily check whether it has defined any objects, e.g. https://stackoverflow.com/questions/7341865 – david25272 May 25 '17 at 23:45
  • I recognise your point, but the solution I am looking for must be generic and can make no assumptions about side effects. – Niels B. May 26 '17 at 08:32

0 Answers0