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 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 $.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 // Attach a bunch of functions for handling common AJAX events
86 var e = "ajaxStart.ajaxComplete.ajaxError.ajaxSuccess".split(',');
88 for ( var i = 0; i < e.length; i++ ){ (function(){
90 $.fn[o] = function(f){return this.bind(o, f);};
95 * A common wrapper for making XMLHttpRequests
97 $.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 // Create the request object
110 var xml = new XMLHttpRequest();
113 xml.open(type || "GET", url, true);
115 // Set the correct header, if data is being sent
117 xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
119 // Set header so calling script knows that it's an XMLHttpRequest
120 xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
122 // Make sure the browser sends the right content length
123 if ( xml.overrideMimeType )
124 xml.setRequestHeader("Connection", "close");
126 // Wait for a response to come back
127 xml.onreadystatechange = function(){
129 if ( xml.readyState == 1 ) {
134 $.event.trigger( "ajaxStart" );
137 // Socket is closed and data is available
138 if ( xml.readyState == 4 ) {
139 // Hide loader if needed
140 if ( ! --$.ajax.active ) {
141 $.event.trigger( "ajaxComplete" );
145 // Make sure that the request was successful
146 if ( $.httpSuccess( xml ) ) {
148 // If a local callback was specified, fire it
149 if ( success ) success( xml );
151 // Fire the global callback
152 $.event.trigger( "ajaxSuccess" );
154 // Otherwise, the request was not successful
156 // If a local callback was specified, fire it
157 if ( error ) error( xml );
159 // Fire the global callback
160 $.event.trigger( "ajaxError" );
172 // Counter for holding the number of active queries
175 // Determines if an XMLHttpRequest was successful or not
176 $.httpSuccess = function(r) {
177 return ( r.status && ( r.status >= 200 && r.status < 300 ) ||
178 r.status == 304 ) || !r.status && location.protocol == "file:";
181 // Get the data out of an XMLHttpRequest
182 $.httpData = function(r,type) {
183 // Check the headers, or watch for a force override
184 return r.getResponseHeader("content-type").indexOf("xml") > 0 ||
185 type == "xml" ? r.responseXML : r.responseText;
188 // Serialize an array of form elements or a set of
189 // key/values into a query string
190 $.param = function(a) {
193 // If an array was passed in, assume that it is an array
195 if ( a.constructor == Array )
196 // Serialize the form elements
197 for ( var i = 0; i < a.length; i++ )
198 s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
200 // Otherwise, assume that it's an object of key/value pairs
202 // Serialize the key/values
204 s.push( j + "=" + encodeURIComponent( a[j] ) );
206 // Return the resulting serialization