How to load local jQuery file in case CDN is not available ?
Last Updated :
25 Aug, 2021
jQuery is a lightweight, "write less, do more" JavaScript library. jQuery helps to write JavaScript as simply as easily possible. It is effective in converting a few lines of code in JavaScript to one single line. It also simplifies tasks like Ajax calls and DOM (Document Object Model).
The jQuery library contains the following features:
- HTML/DOM manipulation
- CSS manipulation
- HTML event methods
- Effects and animations
- AJAX
- Utilities
There are a number of ways you can use jQuery in your application, one being easier than the others. Below are some of the methods that you can use to load local jquery file.
Method 1: Using jQuery CDN (Content Delivery Network)
Example: In this example, we will simply include the CDN line in the script tag and If you click the submit button you will see your name coming up as alert in the browser.
HTML
<!DOCTYPE html>
<html>
<head>
<!--Script loaded from Google CDN service-->
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
</head>
<body>
<h2 style="color:green">GeeksforGeeks</h2>
<p>Using the CDN link for jQuery</p>
<input type="text" placeholder="Name...." id="name">
<button id="btn">Submit</button>
<script>
$("#btn").click(function (event) {
// If you click the submit button
// you will see your name coming
// up as alert in the browser
event.preventDefault();
var name = $("#name").val();
alert(name);
});
</script>
</body>
</html>
Output:
With CDN link
Now we will see how to load the local jquery file in case if above CDN is not available.
Method 2: Download jQuery to use it locally.
Downloading the jQuery library from here. There are two versions of jQuery you can download.
- Production version: This is for your live website because it has been minified and compressed
- Development version: This is for testing and development (uncompressed and readable code)
Download jquery library link from this page- The downloaded file will be a single JavaScript file that you can reference as shown. While saving in your current folder, rename it and include the same name in your header section while including the library file.
Example: In this example, instead of using CDN we will download library file "jquery.min.js" and If you click the submit button you will see your name coming up as alert in the browser.
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
</script>
<!--In case cdn fails, local will load up for sure-->
<script>
window.jQuery || document.write(
'<script src="jquery.min.js">\x3C/script>'
)
</script>
<!--'\x3C' has been used so that the script
does not end prematurely. The above
condition checks whether local has
loaded if not then it loads up the
local -->
</head>
<body>
<h2 style="color:green">GeeksforGeeks</h2>
<p>
This works with library downloaded
in the current folder
</p>
<input type="text" placeholder="Name...." id="name">
<button id="btn">Submit</button>
<script>
$("#btn").click(function (event) {
// If you click the submit button you
// will see your name coming up as
// alert in the browser
event.preventDefault();
var name = $("#name").val();
alert(name);
});
</script>
</body>
</html>
Output:
With a downloaded jquery library
Note: The path in the source (src) of the script tag needs to be configured properly. It should be set to the right path for jQuery to work perfectly.
You can also install jQuery as a node package using npm (node package installer).
npm install jquery
You can also install it using the yarn CLI, the command being as follows.
yarn add jquery
Similar Reads
How to load jQuery locally when CDN fails ? In this article, we will learn how to load our local jQuery file when CDN fails. First of all, why we should use CDN if there is a chance that it fails? There are many advantages of using CDN rather than local jQuery files. Some advantages of using CDN are as follows. It reduces the load from our se
3 min read
How to check if a jQuery plugin is loaded? There are multiple ways by which we can simply check whether the jQuery plugins are loaded successfully or not using jQuery. We can also check whether a particular function within the plugin is accessible or not. This tutorial will demonstrate how to check if a jQuery plugin is loaded or not. Step 1
3 min read
How to load jQuery code after loading the page? In this article, we are going to discuss the different ways of loading the jQuery code after loading the page. The methods to accomplish this task are listed below: Table of Content Using the on() method with the load eventUsing the ready() methodUsing the load() methodMethod 1: Using the on() metho
3 min read
Check if a jquery has been loaded or not Given a document, The task is to determine whether JQuery has been loaded or not, If not then load it dynamically using JavaScript. Below are the examples to check: Approach: First, check if it is loaded.If not loaded then load it dynamically, create a script element and add properties like(type, sr
2 min read
How to run a code on document ready event in jQuery ? In this article, we will see how to run a code when a page loads using jQuery. To run a code at page load time, we will use the ready() method. This method helps to load the whole page and then execute the rest code. This method specifies the function to execute when the DOM is fully loaded. Syntax:
1 min read