How to load local jQuery file in case CDN is not available?

 Posted by Sheonarayan on 7/20/2011 | Category: jQuery Interview questions | Views: 4512 | Points: 40

Sometimes, it may happen that the CDN you have used to load the jQuery file is not available (it rarely happens, however anything is possible, isn?t it?); in that case you should load your local jQuery file that is available on your server so that all jQuery related functionality still work on your page.

Write following lines of code

<!-- START - jQuery Reference -->
<script type="text/javascript" language="Javascript" src=" "></script>

<script type='text/javascript'>//<![CDATA[
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/Script/jquery-1.4.1.min.js' type='text/javascript' %3E%3C/script%3E"));

<!-- END - jQuery Reference -->

Replace bolded path with your own jQuery file path on the server. In the above code, first line tries to load the jQuery file from CDN, if browser could load the file successfully, “jQuery” variable will not be undefined and next script will not run otherwise next script will run that will write the script tag to load the jQuery file from your server.

Source: | | Alert Moderator 

Comments or Responses

Login to post response