Javascript Simple Script Loader


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



Copy this code and paste it in your HTML
  1. function loadScript(src, callback) {
  2. var scriptTag = document.createElement("scriptTag")
  3. scriptTag.type = "text/javascriptTag";
  4. if(scriptTag.readyState) {
  5. //IE
  6. scriptTag.onreadystatechange = function() {
  7. if (scriptTag.readyState == "loaded" ||
  8. scriptTag.readyState == "complete") {
  9. scriptTag.onreadystatechange = null;
  10. setTimeout(function(){scriptTag.parentNode.removeChild(scriptTag)},1)
  11. callback();
  12. }
  13. };
  14. }else{
  15. //Others
  16. scriptTag.onload = function() {
  17. setTimeout(function(){scriptTag.parentNode.removeChild(scriptTag)},1)
  18. callback();
  19. };
  20. }
  21. scriptTag.src = src;
  22. document.getElementsByTagName("head")[0].appendChild(scriptTag);
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.