
The jQuery CDN is a way to include jQuery in your website without actually downloading and keeping it your website’s folder. There are a number of jQuery CDNs which you can use, examples – Google, Microsoft, Cloudflare, jQuery own CDN & more.
Normally we all first download & put the jQuery file in the website’s folder. Then we reference this file on the page head section.
<head>
<script src="files/jquery-3.7.1.min.js"></script>
</head>
Instead we can use jQuery CDN by just providing the link of jQuery directly from Google, Microsoft, CloudFlare, etc.
So when using jQuery from the jQuery’s own CDN, you can do:
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
Notice the src path that links to jQuery file from code.jquery.com domain.
In the same way we can use jQuery CDN hosted by Google, as shown below:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
Microsoft also hosts jQuery and this CDN is used below:
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.7.1.min.js"></script>
</head>
CloudFlare is a great CDN and also very popular too. It also has jQuery CDN which you can use in your website.
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
When you use a jQuery CDN, you get multiple key benefits:
If the jQuery CDN is ever down or blocked, your site’s jQuery features could break unless you add a local fallback. We will see this thing next.
The article What is CDN? explains all about CDN from starting to the end. Do check it.
Fortunately, there’s a simple fix for these CDN-related issues: you can set up a locally-hosted fallback version of jQuery. The basic idea behind a CDN fallback is to check whether a specific type or variable exists after the script has loaded. If it isn’t found, the browser then loads the jQuery file from your own server instead.
Notice the escape characters inside the document.write method.
<!-- First try loading jQuery from CDN -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<!-- If the CDN fails then fallback to a local copy of jQuery -->
<script>
window.jQuery || document.write('<script src="/js/jquery-3.7.1.min.js"><\/script>')
</script>
window.jQuery checks if jQuery has already been loaded into the browser. When jQuery loads successfully (from the CDN <script> tag above it), it creates a global jQuery object attached to window. So window.jQuery will either be:
undefined (if the CDN failed/was blocked) → falsyJavaScript’s || uses short-circuit evaluation: it only runs the right-hand side if the left-hand side is falsy.
undefined) → the right side runs.document.write('<script src="/js/jquery-3.7.1.min.js"><\/script>')This injects a new <script> tag into the page that loads jQuery from your local server instead.
<\/script>The backslash before the forward slash (\/) is there to escape it. Without this, the browser’s HTML parser would see </script> inside the string and think the current script block is ending early — which would break the page. Escaping it as <\/script> prevents that misinterpretation.