Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute function after script injected via document.write has finished loading?

Tags:

javascript

Is it possible to know when a script injected via document.write has finished loading?

document.write('<script src="' + url + '"><\/script>');  

This is executed on page load.

Edit:

Thanks for all the help guys, really helped point me toward the right direction. I ended up using this solution from MSDN

s = document.createElement("script");
s.src="myscript.js";
if(s.addEventListener) {
  s.addEventListener("load",callback,false);
} 
else if(s.readyState) {
  s.onreadystatechange = callback;
}
document.body.appendChild(s);
function callback() { console.log("loaded"); }
like image 675
CheapSteaks Avatar asked Feb 09 '26 17:02

CheapSteaks


1 Answers

Listen to the readystatechange event for IE, and the load event for other browsers.

like image 79
Gareth Bowen Avatar answered Feb 14 '26 18:02

Gareth Bowen



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!