Advertisement
dwhitzzz

LoadScript By Js

Nov 24th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function loadScript(url, callback)
  2. {
  3.     // Adding the script tag to the head as suggested before
  4.     var head = document.getElementsByTagName('head')[0];
  5.     var script = document.createElement('script');
  6.     script.type = 'text/javascript';
  7.     script.src = url;
  8.  
  9.     // Then bind the event to the callback function.
  10.     // There are several events for cross browser compatibility.
  11.     script.onreadystatechange = callback;
  12.     script.onload = callback;
  13.  
  14.     // Fire the loading
  15.     head.appendChild(script);
  16. }
  17. Then you write the code you want to use AFTER the script is loaded in a lambda function:
  18.  
  19. var myPrettyCode = function() {
  20.  
  21.    // Here, do what ever you want
  22. };
  23. Then you run all that:
  24.  
  25. loadScript("my_lovely_script.js", myPrettyCode);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement