3 // http://jquery.com/docs/ajax/
6 * Load HTML from a remote file and inject it into the DOM
8 $.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 = $.param( params );
34 // Request the remote document
35 $.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 $.apply( self, callback, [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 $.get = function( url, callback, type ) {
59 // Build and start the HTTP Request
60 $.ajax( "GET", url, null, function(r) {
61 if ( callback ) callback( $.httpData(r,type) );
66 * Load a remote page using a POST request.
68 $.post = function( url, data, callback, type ) {
69 // Build and start the HTTP Request
70 $.ajax( "POST", url, $.param(data), function(r) {
71 if ( callback ) callback( $.httpData(r,type) );
75 // If IE is used, create a wrapper for the XMLHttpRequest object
76 if ( $.browser == "msie" )
77 XMLHttpRequest = function(){
78 return new ActiveXObject(
79 (navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
80 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
84 // Counter for holding the number of active queries
87 // Attach a bunch of functions for handling common AJAX events
89 var e = ['ajaxStart','ajaxComplete','ajaxError','ajaxSuccess'];
91 for ( var i = 0; i < e.length; i++ ){ (function(){
93 $.fn[o] = function(f){return this.bind(o, f);};
98 * A common wrapper for making XMLHttpRequests
100 $.ajax = function( type, url, data, ret ) {
101 // If only a single argument was passed in,
102 // assume that it is a object of key/value pairs
105 var success = type.success;
106 var error = type.error;
112 // Create the request object
113 var xml = new XMLHttpRequest();
116 xml.open(type || "GET", url, true);
118 // Set the correct header, if data is being sent
120 xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
122 // Set header so calling script knows that it's an XMLHttpRequest
123 xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
125 // Make sure the browser sends the right content length
126 if ( xml.overrideMimeType )
127 xml.setRequestHeader('Connection', 'close');
129 // Wait for a response to come back
130 xml.onreadystatechange = function(){
132 if ( xml.readyState == 1 ) {
136 // Show loader if needed
137 if ( $.xmlActive >= 1 && $.xmlCreate )
138 $.event.trigger( 'ajaxStart' );
141 // Socket is closed and data is available
142 if ( xml.readyState == 4 ) {
146 // Hide loader if needed
147 if ( $.xmlActive <= 0 && $.xmlDestroy ) {
148 $.event.trigger( 'ajaxComplete' );
152 // Make sure that the request was successful
153 if ( $.httpSuccess( xml ) ) {
155 // If a local callback was specified, fire it
156 if ( success ) success( xml );
158 // Fire the global callback
159 $.event.trigger( 'ajaxSuccess' );
161 // Otherwise, the request was not successful
163 // If a local callback was specified, fire it
164 if ( error ) error( xml );
166 // Fire the global callback
167 $.event.trigger( 'ajaxError' );
179 // Determines if an XMLHttpRequest was successful or not
180 $.httpSuccess = function(r) {
181 return ( r.status && ( r.status >= 200 && r.status < 300 ) ||
182 r.status == 304 ) || !r.status && location.protocol == 'file:';
185 // Get the data out of an XMLHttpRequest
186 $.httpData = function(r,type) {
187 // Check the headers, or watch for a force override
188 return r.getResponseHeader("content-type").indexOf("xml") > 0 ||
189 type == "xml" ? r.responseXML : r.responseText;
192 // Serialize an array of form elements or a set of
193 // key/values into a query string
194 $.param = function(a) {
197 // If an array was passed in, assume that it is an array
199 if ( a.constructor == Array )
200 // Serialize the form elements
201 for ( var i = 0; i < a.length; i++ )
202 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
204 // Otherwise, assume that it's an object of key/value pairs
206 // Serialize the key/values
208 s.push( j + "=" + encodeURIComponent( a[j] ) );
210 // Return the resulting serialization