Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Detecting script load completion

Tags:

javascript

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?

like image 589
Archer Avatar asked Aug 01 '26 07:08

Archer


1 Answers

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 = ...
like image 97
Maximillian Laumeister Avatar answered Aug 03 '26 19:08

Maximillian Laumeister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!