Tuesday, 11 April 2017

How to load jQuery Library when CDN fails

How to load jQuery Library when CDN fails
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. Server may have reason to get down, so we need to be take care of these problems.

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.


if (typeof jQuery == 'undefined'{
console.log(" Jquery undefined");
  document.write(unescape("%3Cscript src='MyScripts/jquery.1.12.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}



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.

No comments:

Post a Comment