1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
<div class="entry-title roundTop">
<h1 class="jq-clearfix">.load()</h1>
<div class="entry-meta jq-clearfix">
Categories:
<span class="category"><a href="http://api.jquery.com/category/ajax/" title="View all posts in Ajax">Ajax</a> > <a href="http://api.jquery.com/category/ajax/shorthand-methods/" title="View all posts in Shorthand Methods">Shorthand Methods</a></span>
</div>
</div>
<div id="load1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#jQuery">jQuery</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Load data from the server and place the returned HTML into the matched element.</p>
<ul class="signatures"><li class="signature" id="load-url-data-completeresponseText- textStatus- XMLHttpRequest">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )</h4>
<p class="arguement"><strong>url</strong>A string containing the URL to which the request is sent.</p>
<p class="arguement"><strong>data</strong>A map or string that is sent to the server with the request.</p>
<p class="arguement"><strong>complete(responseText, textStatus, XMLHttpRequest)</strong>A callback function that is executed when the request completes.</p>
</li></ul>
<div class="longdesc">
<p>This method is the simplest way to fetch data from the server. It is roughly equivalent to <code>$.get(url, data, success)</code> 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 <code>textStatus</code> is "success" or "notmodified"), <code>.load()</code> sets the HTML contents of the matched element to the returned data. This means that most uses of the method can be quite simple:</p>
<pre>$('#result').load('ajax/test.html');</pre>
<p>The provided callback, if any, is executed after this post-processing has been performed:</p>
<pre>$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});</pre>
<p>In the two examples above, if the current document does not contain an element with an ID of "result," the <code>.load()</code> method is not executed.</p>
<p>The POST method is used if data is provided as an object; otherwise, GET is assumed.</p>
<blockquote><p>Note: The event handling suite also has a method named <code><a href="/load-event">.load()</a></code>. Which one is fired depends on the set of arguments passed.</p></blockquote>
<h4>Loading Page Fragments</h4>
<p>The <code>.load()</code> method, unlike <code><a href="/jQuery.get">$.get()</a></code>, allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the <code>url</code> 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. </p>
<p>We could modify the example above to use only part of the document that is fetched:</p>
<pre>$('#result').load('ajax/test.html #container');</pre>
<p>When this method executes, it retrieves the content of <code>ajax/test.html</code>, but then jQuery parses the returned document to find the element with an ID of <code>container</code>. This element, along with its contents, is inserted into the element with an ID of <code>result</code>, and the rest of the retrieved document is discarded.</p>
<p>jQuery uses the browser's <code>.innerHTML</code> property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <code><html></code>, <code><title></code>, or <code><head></code> elements. As a result, the elements retrieved by <code>.load()</code> may not be exactly the same as if the document were retrieved directly by the browser.</p>
<blockquote><p><strong>Note:</strong> When calling <code>.load()</code> using a URL without a suffixed selector expression, the content is passed to <code>.html()</code> prior to scripts being removed. This executes the script blocks before they are discarded. If <code>.load()</code> is however called with a selector expression appended to the URL, the scripts are stripped out prior to the DOM being updated, which is why they are never executed. An example of both cases can be seen below:
</p></blockquote>
<p>Here, any JavaScript loaded into <code>#a</code> as a part of the document will successfully execute.</p>
<pre>
$('#a').load('article.html');
</pre>
<p>However in this case, script blocks in the document being loaded into <code>#b</code> are stripped out prior to being executed:</p>
<pre>
$('#b').load('article.html #target');
</pre>
</div>
<h3 id="notes-0">Additional Notes:</h3>
<div class="longdesc"><ul><li>Due to browser security restrictions, most "Ajax" requests are subject to the <a title="Same Origin Policy on Wikipedia" href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>; the request can not successfully retrieve data from a different domain, subdomain, or protocol.</li></ul></div>
<h3>Examples:</h3>
<div class="entry-examples" id="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">Load the main page's footer navigation into an ordered list.</span>
</h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<b>Footer navigation:</b>
<ol id="new-nav"></ol>
<script>
$("#new-nav").load("/ #jq-footerNavigation li");
</script>
</body>
</html></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-1">
<h4>Example: <span class="desc">Display a notice if the Ajax request encounters an error.</span>
</h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.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></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-2">
<h4>Example: <span class="desc">Load the feeds.html file into the div with the ID of feeds.</span>
</h4>
<pre class="prettyprint"><code class="example">$("#feeds").load("feeds.html");</code></pre>
<h4>Result:</h4>
<pre><code class="results"><div id="feeds"><b>45</b> feeds found.</div></code></pre>
</div>
<div id="example-3">
<h4>Example: <span class="desc">pass arrays of data to the server.</span>
</h4>
<pre class="prettyprint"><code class="example">$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );</code></pre>
</div>
<div id="example-4">
<h4>Example: <span class="desc">Same as above, but will POST the additional parameters to the server and a callback that is executed when the server is finished responding.</span>
</h4>
<pre class="prettyprint"><code class="example">$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});</code></pre>
</div>
</div>
</div>
</div>
</div>
</body></html>
|