Load jQuery dynamically from Javascript


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

Load jQuery dynamically in a page from Javascript


Copy this code and paste it in your HTML
  1. // Anonymous "self-invoking" function
  2. (function() {
  3. // Load the script
  4. var script = document.createElement("SCRIPT");
  5. script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
  6. script.type = 'text/javascript';
  7. document.getElementsByTagName("head")[0].appendChild(script);
  8.  
  9. // Poll for jQuery to come into existance
  10. var checkReady = function(callback) {
  11. if (window.jQuery) {
  12. callback(jQuery);
  13. }
  14. else {
  15. window.setTimeout(function() { checkReady(callback); }, 100);
  16. }
  17. };
  18.  
  19. // Start polling...
  20. checkReady(function($) {
  21. // Use $ here...
  22. });
  23. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.