Advertisement
Gerst20051

Load jQuery If Not Loaded

Aug 1st, 2011
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // My Solution To Load jQuery From Your Script File If It Isn't Already Loaded!!!!!
  2.  
  3. window.$ && main() || (function() {
  4.     var jquery = document.createElement("script");
  5.     jquery.setAttribute("type","text/javascript");
  6.     jquery.setAttribute("src","jquery.js");
  7.     jquery.onload = main;
  8.     jquery.onreadystatechange = function() {
  9.         if (this.readyState == "complete" || this.readyState == "loaded") main();
  10.     };
  11.     (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(jquery);
  12. })();
  13.  
  14. function main() {
  15. // run your website's script
  16. }
  17.  
  18.  
  19. // jQuery's Implementation Of This On Wikipedia [ Just Noticed This :( ]
  20. // Just to make things clear this does not serve the same purpose as my solution and wouldn't work without some modification.
  21. http://en.wikipedia.org/wiki/JQuery
  22.  
  23. if(!(window.jQuery && window.jQuery.fn.jquery == '1.6.2')) {
  24.     var s = document.createElement('script');
  25.     s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
  26.     s.setAttribute('type', 'text/javascript');
  27.     document.getElementsByTagName('head')[0].appendChild(s);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement