Q:

How JS file can be download programmatically

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.

A:

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/)

A:

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>

