In the jQuery load() method, a simple way to load data asynchronous from a web server is provided in the sense that specific data from the server is loaded and the returned HTML is placed into the selected element. The basic syntax of this method is given below:
$(selector).load(URL, data, complete);
Below is the meaning of the parameters of the above syntax:
Observe the following instructions to use the jQuery load() method:
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading Data from External File using Ajax in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#box").load("/examples/html/test-content.html");
});
});
</script>
</head>
<body>
<div id="box">
<h2>Click button to load new content inside DIV box</h2>
</div>
<button type="button">Load Content</button>
</body>
</html>
Example 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading Data from External File using Ajax in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#box").load("/examples/html/test-content.html");
});
});
</script>
</head>
<body>
<div id="box">
<h2>Click button to load new content inside DIV box</h2>
</div>
<button type="button">Load Content</button>
</body>
</html>
Furthermore, the callback function can have three different parameters and they are as follows:
Below is the modified version of the previous example that will display either the success or error message to the user depending on the status of the request.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Showing Ajax Load Request Status in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#box").load("/examples/html/test-content.html", function(responseTxt, statusTxt, jqXHR){
if(statusTxt == "success"){
alert("New content loaded successfully!");
}
if(statusTxt == "error"){
alert("Error: " + jqXHR.status + " " + jqXHR.statusText);
}
});
});
});
</script>
</head>
<body>
<div id="box">
<h2>Click button to load new content inside DIV box</h2>
</div>
<button type="button">Load Content</button>
</body>
</html>
The jQuery load() also permits only a portion of the document to be fetched and this is clearly achieved by appending the URL parameter with a space followed by a jQuery selector.
Below is a clear picture of loading page fragments.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading a Portion of External Page using Ajax in jQuery</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#box").load("/examples/html/test-content.html #hint");
});
});
</script>
</head>
<body>
<div id="box">
<h2>Click button to load new content inside DIV box</h2>
</div>
<button type="button">Load Content</button>
</body>
</html>
The jQuery selector #hint indicates the portion of the "test-content.html" file to be inserted inside the DIV box, which is an element that has the ID feature with a value hint i.e. id="hint" within the URL parameter (line no-10), check the first example.