How to load jQuery locally when CDN fails?



How to load jQuery locally when CDN fails?..

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

More jQuery Interview Questions

Is vanilla js faster than jquery?

0 Answers  


What is $() in jquery library?

0 Answers  


Will Events Are Also Copied On Clone In Jquery?

0 Answers  


What are the advantages of method chaining in jquery?

0 Answers  


How do I check if an HTML element is empty using jQuery?

0 Answers  






Is it too early to discuss jquery ui 1.8?

0 Answers  


Explain deferred and promise object in jquery? : jquery mobile

0 Answers  


What is jquery and what are the advantages?

0 Answers  


What is cdn and how jquery is related to it?

0 Answers  


How to increase cursor size in html body using jquery?

0 Answers  


What is the use of queue() in jquery?

0 Answers  


Which version of jquery file should be used?

0 Answers  


Categories