3 // http://jquery.com/docs/ajax/
6 * Load HTML from a remote file and inject it into the DOM
8 jQuery.prototype.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 || "");
56 * Load a remote page using a GET request
58 jQuery.get = function( url, callback, type ) {
59 // Build and start the HTTP Request
60 jQuery.ajax( "GET", url, null, function(r) {
61 if ( callback ) callback( jQuery.httpData(r,type) );
66 * Load a remote page using a POST request.
68 jQuery.post = function( url, data, callback, type ) {
69 // Build and start the HTTP Request
70 jQuery.ajax( "POST", url, jQuery.param(data), function(r) {
71 if ( callback ) callback( jQuery.httpData(r,type) );
75 // If IE is used, create a wrapper for the XMLHttpRequest object
76 if ( jQuery.browser == "msie" )
77 XMLHttpRequest = function(){
78 return new ActiveXObject(
79 (navigator.userAgent.toLowerCase().indexOf("msie 5") >= 0) ?
80 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
84 // Attach a bunch of functions for handling common AJAX events
86 var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
88 for ( var i = 0; i < e.length; i++ ){ (function(){
90 jQuery.fn[o] = function(f){return this.bind(o, f);};
95 * A common wrapper for making XMLHttpRequests
97 jQuery.ajax = function( type, url, data, ret ) {
98 // If only a single argument was passed in,
99 // assume that it is a object of key/value pairs
102 var success = type.success;
103 var error = type.error;
109 // Watch for a new set of requests
110 if ( ! jQuery.ajax.active++ )
111 jQuery.event.trigger( "ajaxStart" );
113 // Create the request object
114 var xml = new XMLHttpRequest();
117 xml.open(type || "GET", url, true);
119 // Set the correct header, if data is being sent
121 xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
123 // Set header so calling script knows that it's an XMLHttpRequest
124 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
126 // Make sure the browser sends the right content length
127 if ( xml.overrideMimeType )
128 xml.setRequestHeader("Connection", "close");
130 // Wait for a response to come back
131 xml.onreadystatechange = function(){
132 // The transfer is complete and the data is available
133 if ( xml.readyState == 4 ) {
134 // Make sure that the request was successful
135 if ( jQuery.httpSuccess( xml ) ) {
137 // If a local callback was specified, fire it
138 if ( success ) success( xml );
140 // Fire the global callback
141 jQuery.event.trigger( "ajaxSuccess" );
143 // Otherwise, the request was not successful
145 // If a local callback was specified, fire it
146 if ( error ) error( xml );
148 // Fire the global callback
149 jQuery.event.trigger( "ajaxError" );
152 // The request was completed
153 jQuery.event.trigger( "ajaxComplete" );
155 // Handle the global AJAX counter
156 if ( ! --jQuery.ajax.active )
157 jQuery.event.trigger( "ajaxStop" );
168 // Counter for holding the number of active queries
169 jQuery.ajax.active = 0;
171 // Determines if an XMLHttpRequest was successful or not
172 jQuery.httpSuccess = function(r) {
173 return ( r.status && ( r.status >= 200 && r.status < 300 ) ||
174 r.status == 304 ) || !r.status && location.protocol == "file:";
177 // Get the data out of an XMLHttpRequest.
178 // Return parsed XML if content-type header is "xml" and type is "xml" or omitted,
179 // otherwise return plain text.
180 jQuery.httpData = function(r,type) {
181 var xml = ( !type || type == "xml" ) &&
182 r.getResponseHeader("content-type").indexOf("xml") >= 0;
183 return xml ? r.responseXML : r.responseText;
186 // Serialize an array of form elements or a set of
187 // key/values into a query string
188 jQuery.param = function(a) {
191 // If an array was passed in, assume that it is an array
193 if ( a.constructor == Array )
194 // Serialize the form elements
195 for ( var i = 0; i < a.length; i++ )
196 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
198 // Otherwise, assume that it's an object of key/value pairs
200 // Serialize the key/values
202 s.push( j + "=" + encodeURIComponent( a[j] ) );
204 // Return the resulting serialization