jQuery API

.load()

.load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] ) Returns: jQuery

Description: Load data from the server and place the returned HTML into the matched element.

  • version added: 1.0.load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )

    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.

Loading Page Fragments

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.

Examples:

Example: Load a piece of the documentation sidebar navigation into a custom unordered list.

<!DOCTYPE html>
<html>
<head>
  <style>body{ font-size: 11px; font-family: Arial; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<b>Main Page Header:</b> <span id="header"></span>
<script>$("#header").load("/ h1");</script>
</body>
</html>

Demo:

Example: Load the feeds.html file into the div with the ID of feeds.

$("#feeds").load("feeds.html");

Result:

<div id="feeds"><b>45</b> feeds found.</div>

Example: pass arrays of data to the server.

$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );

Example: Same as above, but will POST the additional parameters to the server and a callback that is executed when the server is finished responding.

$("#feeds").load("feeds.php", {limit: 25}, function(){
   alert("The last 25 entries in the feed have been loaded");
 });

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .load() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Val
    It's mentioned that only a “string” or a “map/object” can be used as the value for the data parameter but not an array. However, as load() calls ajax() and the latter transforms a data array into a query string, it should be mentioned here that data can also be an array.
  • Cyp
    the load function does not grab script tags. it should grab everything.
  • The .load() method has always stripped out script tags. I doubt it's going to change now. Here is the explanation in the core file:
    // inject the contents of the document in, removing the scripts
    // to avoid any 'Permission Denied' errors in IE

    However, if you think you can make a compelling case for their inclusion, go for it. But, please, do so on the jQuery forum, not here.
  • Thats, Bull Sorry but we have uploadify loaded though a .load() event and it loads fine, however the browser might remove it if you have not specifyed it correctly,

    When loading a from a .js Source you must have </script> not just /> it is not a XHTML usage tag if that is still not your problem try loading the ajax page in your browser see if you get any script errors if this is the case there your problem,

    The only reason i am saying this is if any one read this and thinks it cant do some thing it can do next time Cyp post in the forum not the comments
  • Martin,
    1. Don't shoot the messenger.
    2. "If you think you can make a compelling case for their inclusion, go for it. But, please, do so on the jQuery forum, not here."
  • pete
    i think it is worth mentioning that some browsers don't accept loading a page from another source. ie, in firefox you can't use .load("http://www.abc.com"); in safari it does seem to work.
  • pete
    further to this. it seems this is a client side security feature to prevent phishing.
    to get around this, i loaded the page from another domain on my server, then echoed it out. that way the domain stayed the same and the feature worked.
  • Stuart
    Be mindful of no cache in header with IE. Or use an inject of random number like
    $("#objectID").load("test.php?n="+random, { 'choices[]': ["Jon", "Susan"] } );