3

I'm dynamically adding script usign:

var el = document.createElement("script");
document.getElementsByTagname("head")[0].appendChild(el);

It seems neither script.onload nor document.onreadystatechange could be used to determine the end of loading process. How should I catch dynamic script load completion?

Archer
  • 5,073
  • 8
  • 50
  • 96
  • I think you want something very similar to this: http://stackoverflow.com/questions/16230886/trying-to-fire-onload-event-on-script-tag#16231055 – DDan Aug 06 '15 at 05:32
  • possible duplicate of [Include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file) – Madness Aug 06 '15 at 05:33

2 Answers2

3

The onload event needs to be attached before setting the script's src, which is what causes the script to start loading.

Example:

var el = document.createElement("script");
el.onload = function() {
    // Script is loaded
}
el.src = ...
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
1

I think you want something very similar to this: Trying to fire the onload event on script tag

$body.append(yourDynamicScriptElement);
yourDynamicScriptElement.onload = function() { //...
yourDynamicScriptElement.src = script;
Community
  • 1
  • 1
DDan
  • 8,068
  • 5
  • 33
  • 52