Loading Scripts without Blocking


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.