Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.24 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Dynamically Loading JQuery form an externally linked to JavaScript file
  2. if (typeof jQuery == 'undefined') {
  3.   //alert('No jQuery! Will attempt to load it now');
  4.   var script = document.createElement('script');
  5.   script.type = "text/javascript";
  6.   script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
  7.   document.getElementsByTagName('head')[0].appendChild(script);
  8.   tryReady = function(time_elapsed) {
  9.     if (typeof jQuery == "undefined") { // if jQuery isn't loaded yet...
  10.       if (time_elapsed <= 100000) { // and we havn't given up trying...
  11.         setTimeout("tryReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
  12.       } else {
  13.         alert('hello'); // If this isn't here, the subsequent scripts fail to access jQuery
  14.       }
  15.     } else {
  16.       //alert ("jQuery wasn't but now is loaded")
  17.     }
  18.   }
  19.   tryReady();
  20. }      
  21.   // Functions making use of jQuery go here...
  22.        
  23. if (typeof jQuery == 'undefined') {
  24.   var script = document.createElement('script');
  25.   script.type = "text/javascript";
  26.   script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
  27.   script.onload = function(){
  28.     alert('voila');
  29.   }
  30.   document.getElementsByTagName('head')[0].appendChild(script);
  31. }