3 // http://jquery.com/docs/ajax/
6 * Load HTML from a remote file and inject it into the DOM
8 jQuery.fn.load = function( url, params, callback ) {
9 // I overwrote the event plugin's .load
10 // this won't happen again, I hope -John
11 if ( url && url.constructor == Function )
12 return this.bind("load", url);
14 // Default to a GET request
17 // If the second parameter was provided
20 if ( params.constructor == Function ) {
21 // We assume that it's the callback
25 // Otherwise, build a param string
27 params = jQuery.param( params );
34 // Request the remote document
35 jQuery.ajax( type, url, params,function(res){
37 // Inject the HTML into all the matched elements
38 self.html(res.responseText).each(function(){
39 // If a callback function was provided
40 if ( callback && callback.constructor == Function )
41 // Execute it within the context of the element
42 callback.apply( self, [res.responseText] );
45 // Execute all the scripts inside of the newly-injected HTML
46 $("script", self).each(function(){
47 eval( this.text || this.textContent || this.innerHTML || "");
55 // If IE is used, create a wrapper for the XMLHttpRequest object
56 if ( jQuery.browser.msie )
57 XMLHttpRequest = function(){
58 return new ActiveXObject(
59 navigator.userAgent.indexOf("MSIE 5") >= 0 ?
60 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
64 // Attach a bunch of functions for handling common AJAX events
66 var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
68 for ( var i = 0; i < e.length; i++ ) new function(){
70 jQuery.fn[o] = function(f){
71 return this.bind(o, f);
79 * Load a remote page using a GET request
81 get: function( url, data, callback, type ) {
82 if ( data.constructor == Function ) {
88 url += "?" + jQuery.param(data);
90 // Build and start the HTTP Request
91 jQuery.ajax( "GET", url, null, function(r) {
92 if ( callback ) callback( jQuery.httpData(r,type) );
97 * Load a remote page using a POST request.
99 post: function( url, data, callback, type ) {
100 // Build and start the HTTP Request
101 jQuery.ajax( "POST", url, jQuery.param(data), function(r) {
102 if ( callback ) callback( jQuery.httpData(r,type) );
107 * A common wrapper for making XMLHttpRequests
109 ajax: function( type, url, data, ret ) {
110 // If only a single argument was passed in,
111 // assume that it is a object of key/value pairs
114 var success = type.success;
115 var error = type.error;
121 // Watch for a new set of requests
122 if ( ! jQuery.active++ )
123 jQuery.event.trigger( "ajaxStart" );
125 // Create the request object
126 var xml = new XMLHttpRequest();
129 xml.open(type || "GET", url, true);
131 // Set the correct header, if data is being sent
133 xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
135 // Set header so calling script knows that it's an XMLHttpRequest
136 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
138 // Make sure the browser sends the right content length
139 if ( xml.overrideMimeType )
140 xml.setRequestHeader("Connection", "close");
142 // Wait for a response to come back
143 xml.onreadystatechange = function(){
144 // The transfer is complete and the data is available
145 if ( xml.readyState == 4 ) {
146 // Make sure that the request was successful
147 if ( jQuery.httpSuccess( xml ) ) {
149 // If a local callback was specified, fire it
150 if ( success ) success( xml );
152 // Fire the global callback
153 jQuery.event.trigger( "ajaxSuccess" );
155 // Otherwise, the request was not successful
157 // If a local callback was specified, fire it
158 if ( error ) error( xml );
160 // Fire the global callback
161 jQuery.event.trigger( "ajaxError" );
164 // The request was completed
165 jQuery.event.trigger( "ajaxComplete" );
167 // Handle the global AJAX counter
168 if ( ! --jQuery.active )
169 jQuery.event.trigger( "ajaxStop" );
180 // Counter for holding the number of active queries
183 // Determines if an XMLHttpRequest was successful or not
184 httpSuccess: function(r) {
187 ( r.status >= 200 && r.status < 300 ) || r.status == 304 :
188 location.protocol == "file:";
193 // Get the data out of an XMLHttpRequest.
194 // Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
195 // otherwise return plain text.
196 httpData: function(r,type) {
197 var ct = r.getResponseHeader("content-type");
198 var xml = ( !type || type == "xml" ) && ct && ct.indexOf("xml") >= 0;
199 return xml ? r.responseXML : r.responseText;
202 // Serialize an array of form elements or a set of
203 // key/values into a query string
207 // If an array was passed in, assume that it is an array
209 if ( a.constructor == Array )
210 // Serialize the form elements
211 for ( var i = 0; i < a.length; i++ )
212 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
214 // Otherwise, assume that it's an object of key/value pairs
216 // Serialize the key/values
218 s.push( j + "=" + encodeURIComponent( a[j] ) );
220 // Return the resulting serialization