jQuery > Introduction

Load local jQuery file if CDN is not available in jQuery

How to load local jQuery file if CDN is not available?


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

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
//<![CDATA[
(window.jQuery) || document.write('<script src="/Scripts/jquery-2.1.3.min.js"><\/script>');
//]]>
</script>

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.

 Views: 1715 | Post Order: 7



Write for us