Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. importScript: (function (head) {
  2.  
  3. function loadError(error) {
  4. throw new URIError("The script " +
  5. error.target.src + " is not accessible.");}
  6.  
  7. return function (url, callback) {
  8. var existingScript = document.querySelectorAll("script[src='" +
  9. url + "']");
  10. var isNewScript = (existingScript.length == 0);
  11. var script;
  12. if (isNewScript) {
  13. script = document.createElement("script")
  14. script.type = "text/javascript";
  15. }
  16. else {
  17. script = existingScript[0];
  18. }
  19. script.onerror = loadError;
  20. if (script.readyState) { //IE
  21. script.onreadystatechange = function () {
  22. if (script.readyState == "loaded" ||
  23. script.readyState == "complete") {
  24. script.onreadystatechange = null;
  25. if (callback) {
  26. callback(); }
  27. }
  28. };
  29. } else { // others than IE
  30. script.onload = callback; }
  31.  
  32. if (isNewScript) {
  33. script.src = url;
  34. head.appendChild(script); }
  35. }
  36. })(document.head || document.getElementsByTagName("head")[0])
  37.  
  38. importScript("myScript1.js");
  39. importScript("myScript2.js", /* onload function: */
  40. function () { alert("The script has been OK loaded."); });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement