|
10 | 10 | <desc>Register a handler to be called when Ajax requests complete. This is an <a href="http://docs.jquery.com/Ajax_Events">AjaxEvent</a>.</desc> |
11 | 11 | <longdesc> |
12 | 12 | <p>Whenever an Ajax request completes, jQuery triggers the <code>ajaxComplete</code> event. Any and all handlers that have been registered with the <code>.ajaxComplete()</code> method are executed at this time.</p> |
13 | | - <p>To observe this method in action, we can set up a basic Ajax load request:</p> |
| 13 | + <p>To observe this method in action, set up a basic Ajax load request:</p> |
14 | 14 | <pre><code><div class="trigger">Trigger</div> |
15 | 15 | <div class="result"></div> |
16 | 16 | <div class="log"></div> |
17 | 17 | </code></pre> |
18 | | - <p>We can attach our event handler to any element:</p> |
19 | | - <pre><code>$('.log').ajaxComplete(function() { |
20 | | - $(this).text('Triggered ajaxComplete handler.'); |
| 18 | + <p>Attach the event handler to the document:</p> |
| 19 | + <pre><code>$(document).ajaxComplete(function() { |
| 20 | + $( ".log" ).text( "Triggered ajaxComplete handler." ); |
21 | 21 | }); |
22 | 22 | </code></pre> |
23 | | - <p>Now, we can make an Ajax request using any jQuery method:</p> |
24 | | - <pre><code>$('.trigger').click(function() { |
25 | | - $('.result').load('ajax/test.html'); |
| 23 | + <p>Now, make an Ajax request using any jQuery method:</p> |
| 24 | + <pre><code>$( ".trigger" ).click(function() { |
| 25 | + $( ".result" ).load( "ajax/test.html" ); |
26 | 26 | });</code></pre> |
27 | 27 | <p>When the user clicks the element with class <code>trigger</code> and the Ajax request completes, the log message is displayed.</p> |
28 | | - <p><strong>Note:</strong> Because <code>.ajaxComplete()</code> is implemented as a method of jQuery object instances, we can use the <code>this</code> keyword as we do here to refer to the selected elements within the callback function.</p> |
29 | | - <p>All <code>ajaxComplete</code> handlers are invoked, regardless of what Ajax request was completed. If we must differentiate between the requests, we can use the parameters passed to the handler. Each time an <code>ajaxComplete</code> handler is executed, it is passed the event object, the <code>XMLHttpRequest</code> object, and the settings object that was used in the creation of the request. For example, we can restrict our callback to only handling events dealing with a particular URL:</p> |
30 | | - <p><strong>Note:</strong> You can get the returned ajax contents by looking at <code>xhr.responseXML</code> or <code>xhr.responseHTML</code> for xml and html respectively.</p> |
31 | | - <pre><code>$('.log').ajaxComplete(function(e, xhr, settings) { |
32 | | - if (settings.url == 'ajax/test.html') { |
33 | | - $(this).text('Triggered ajaxComplete handler. The result is ' + |
34 | | - xhr.responseHTML); |
| 28 | + <p><strong>Note:</strong> Because <code>.ajaxComplete()</code> is implemented as a method of jQuery object instances, you can use the <code>this</code> keyword to refer to the selected elements within the callback function. <strong>As of jQuery 1.8, however, the <code>.ajaxComplete()</code> method should only be attached to <code>document</code>.</strong></p> |
| 29 | + <p>All <code>ajaxComplete</code> handlers are invoked, regardless of what Ajax request was completed. If you must differentiate between the requests, use the parameters passed to the handler. Each time an <code>ajaxComplete</code> handler is executed, it is passed the event object, the <code>XMLHttpRequest</code> object, and the settings object that was used in the creation of the request. For example, you can restrict the callback to only handling events dealing with a particular URL:</p> |
| 30 | + <pre><code>$(document).ajaxComplete(function(event, xhr, settings) { |
| 31 | + if ( settings.url === "ajax/test.html" ) { |
| 32 | + $( ".log" ).text( "Triggered ajaxComplete handler. The result is " + |
| 33 | + xhr.responseHTML ); |
35 | 34 | } |
36 | 35 | });</code></pre> |
| 36 | + <p><strong>Note:</strong> You can get the returned ajax contents by looking at <code>xhr.responseXML</code> or <code>xhr.responseHTML</code> for xml and html respectively.</p> |
37 | 37 | </longdesc> |
38 | 38 | <note id="ajax-global-false" type="additional" data-title=".ajaxComplete()"/> |
39 | 39 | <example> |
40 | 40 | <desc>Show a message when an Ajax request completes.</desc> |
41 | | - <code><![CDATA[$("#msg").ajaxComplete(function(event,request, settings){ |
42 | | - $(this).append("<li>Request Complete.</li>"); |
| 41 | + <code><![CDATA[$(document).ajaxComplete(function(event,request, settings) { |
| 42 | + $( "#msg" ).append( "<li>Request Complete.</li>" ); |
43 | 43 | });]]></code> |
44 | 44 | </example> |
45 | 45 | <category slug="ajax/global-ajax-event-handlers"/> |
|
0 commit comments