2

most of the time we include script tag in html head tag. like

 <Head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>

I want that I don't want to include js file path rather I need to download js programmatically by jquery. like i will download js file programmatically from http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js and check if the file can not be download or does not exist then i will download the same file from my site like www.my-site.com/js/1.5.2/jquery.min.js.

please help me to do it using jquery.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 1
    Why not just download it from google all the time? And how can we help you with jquery if jquery hasn't been downloaded yet? – Amir Raminfar Aug 13 '11 at 15:34
  • 6
    You want to download jquery using jquery? You would have to download jquery first :) – JB Nizet Aug 13 '11 at 15:35
  • 1
    Dupe of http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-goo – Varun Madiath Aug 13 '11 at 15:40
  • Maybe I'm missing something, but if you're trying to load jquery in the first place you will have to use plain old javascript to load the files until jquery has been loaded. You could probably do this using standard XHR and injecting the file contents into a script tag. Is there a reason you want to favor google's reference vs your own? Edit: jquery recursive dependency already mentioned above :) – nebs Aug 13 '11 at 15:42

2 Answers2

4

I think you want to add script programmetically. This following mechanism allows the rendering engine to immediately render and display the initial view defined in HTML while the JavaScript resources are still being loaded and executed which leads to better user experience

      function loadScript(src, callback) {
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
    done = false;
    script.setAttribute('src', src);
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('charset', 'utf-8');
    script.onload = script.onreadstatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
            done = true;
            script.onload = script.onreadystatechange = null;
                if (callback) {
                    callback();
                }
            }
    }
    head.insertBefore(script, head.firstChild);
}

// load the my-script-file.js and display an alert dialog once the script has been loaded
loadScript('my-script-file.js', function() { ///Loaded, add your javascript here. });

When tags are found in the HTML document the referenced script resources are downloaded and executed before the rendering engine can continue to download other resources which effectively blocks the rendering of the page below the tag. To avoid this blocking behaviour a script tag can be created via a mechanism known as a dynamic script tag injection Reference(http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/)

Pir Abdul
  • 2,274
  • 1
  • 26
  • 35
3

following code will solve your problem

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript">
    if (typeof jQuery == 'undefined') {
       document.write(unescape("%3Cscript src='/Scripts/jquery-1.5.2.min.js' type='text/javascript'%3E%3C/script%3E"));
    }
 </script>
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86