Javascript: detecting script loading completion

I am dynamically adding script usign:

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

      

It looks like script.onload

u document.onreadystatechange

cannot be used to detect the end of the boot process. How do I get a dynamic download completion script?

+3


source to share


2 answers


The event onload

needs to be hooked up before installing the script src

, which causes the script to run.

Example:



var el = document.createElement("script");
el.onload = function() {
    // Script is loaded
}
el.src = ...

      

+3


source


I think you need something very similar to this: Trying to fire an onload event on a script tag



$body.append(yourDynamicScriptElement);
yourDynamicScriptElement.onload = function() { //...
yourDynamicScriptElement.src = script;

      

+1


source







All Articles