Description: Load data from the server and place the returned HTML into the matched element.
urlA string containing the URL to which the request is sent.
dataA map or string that is sent to the server with the request.
complete(responseText, textStatus, XMLHttpRequest)A callback function that is executed when the request completes.
This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success)
except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus
is "success" or "notmodified"), .load()
sets the HTML contents of the matched element to the returned data. This means that most uses of the method can be quite simple:
$('#result').load('ajax/test.html');
The provided callback, if any, is executed after this post-processing has been performed:
$('#result').load('ajax/test.html', function() { alert('Load was performed.'); });
In the two examples above, if the current document does not contain an element with an ID of "result," the .load()
method is not executed.
The POST method is used if data is provided as an object; otherwise, GET is assumed.
Note: The event handling suite also has a method named
.load()
. Which one is fired depends on the set of arguments passed.
The .load()
method, unlike $.get()
, allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url
parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.
We could modify the example above to fetch only part of the document:
$('#result').load('ajax/test.html #container');
When this method executes, it retrieves the content of ajax/test.html
, but then jQuery parses the returned document to find the element with an ID of container
. This element, along with its contents, is inserted into the element with an ID of result
, and the rest of the retrieved document is discarded.
<!DOCTYPE html>
<html>
<head>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<b>Footer navigation:</b>
<ol id="new-nav"></ol>
<script>
$("#new-nav").load("/ #jq-footerNavigation li");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
$("#success").load("/not-here.php", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
</script>
</body>
</html>
$("#feeds").load("feeds.html");
<div id="feeds"><b>45</b> feeds found.</div>
$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );
$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});