load.js (1904B)
1 define( [ 2 "../core", 3 "../core/stripAndCollapse", 4 "../var/isFunction", 5 "../core/parseHTML", 6 "../ajax", 7 "../traversing", 8 "../manipulation", 9 "../selector" 10 ], function( jQuery, stripAndCollapse, isFunction ) { 11 12 "use strict"; 13 14 /** 15 * Load a url into a page 16 */ 17 jQuery.fn.load = function( url, params, callback ) { 18 var selector, type, response, 19 self = this, 20 off = url.indexOf( " " ); 21 22 if ( off > -1 ) { 23 selector = stripAndCollapse( url.slice( off ) ); 24 url = url.slice( 0, off ); 25 } 26 27 // If it's a function 28 if ( isFunction( params ) ) { 29 30 // We assume that it's the callback 31 callback = params; 32 params = undefined; 33 34 // Otherwise, build a param string 35 } else if ( params && typeof params === "object" ) { 36 type = "POST"; 37 } 38 39 // If we have elements to modify, make the request 40 if ( self.length > 0 ) { 41 jQuery.ajax( { 42 url: url, 43 44 // If "type" variable is undefined, then "GET" method will be used. 45 // Make value of this field explicit since 46 // user can override it through ajaxSetup method 47 type: type || "GET", 48 dataType: "html", 49 data: params 50 } ).done( function( responseText ) { 51 52 // Save response for use in complete callback 53 response = arguments; 54 55 self.html( selector ? 56 57 // If a selector was specified, locate the right elements in a dummy div 58 // Exclude scripts to avoid IE 'Permission Denied' errors 59 jQuery( "<div>" ).append( jQuery.parseHTML( responseText ) ).find( selector ) : 60 61 // Otherwise use the full result 62 responseText ); 63 64 // If the request succeeds, this function gets "data", "status", "jqXHR" 65 // but they are ignored because response was set above. 66 // If it fails, this function gets "jqXHR", "status", "error" 67 } ).always( callback && function( jqXHR, status ) { 68 self.each( function() { 69 callback.apply( this, response || [ jqXHR.responseText, status, jqXHR ] ); 70 } ); 71 } ); 72 } 73 74 return this; 75 }; 76 77 } );