3

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"); }
CheapSteaks
  • 4,821
  • 3
  • 31
  • 48

1 Answers1

2

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

Gareth Bowen
  • 949
  • 1
  • 7
  • 12