Posted By

iTony on 07/28/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wizard04


Loading Scripts without Blocking


 / Published in: JavaScript
 

URL: http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/

  1. function loadScript(url, callback){
  2.  
  3. var script = document.createElement("script")
  4. script.type = "text/javascript";
  5.  
  6. if (script.readyState){ //IE
  7. script.onreadystatechange = function(){
  8. if (script.readyState == "loaded" ||
  9. script.readyState == "complete"){
  10. script.onreadystatechange = null;
  11. callback();
  12. }
  13. };
  14. } else { //Others
  15. script.onload = function(){
  16. callback();
  17. };
  18. }
  19.  
  20. script.src = url;
  21. document.getElementsByTagName("head")[0].appendChild(script);
  22. }

Report this snippet  

You need to login to post a comment.