How to load jQuery locally when CDN fails?

Answer Posted / chaitanya

It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen.

Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references the jQuery.js file from your folder.

Hide Copy Code

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

<script type="text/javascript">

if (typeof jQuery == 'undefined')

{

document.write(unescape("%3Cscript src='Scripts/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));

}

</script>

It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the concepts of "$ function" in jquery with an example?

525


What is the difference between map and grep function in jquery?

485


How to multiple AJAX requests be run simultaneously in jQuery?

430


Explain which program is useful for testing jquery?

8688


When can you use jquery?

467






What are the advantage of using cdn? : jquery mobile

470


Explain the difference between parent() and parents() methods in jquery? : jquery mobile

464


Is jquery still relevant 2019?

540


How can I select 20th div with jquery?

483


List the advantages of jquery?

445


Can we call C# code behind using jQuery?

522


How to add and remove css classes to an element using ?

461


How do I check if an element is hidden in jQuery?

498


How we can get the value of a radio button using jquery?

428


What is the use of serialize method in jQuery?

489