About half done documenting the AJAX module.
[jquery.git] / src / ajax / ajax.js
index 87dfc9d..16f7d70 100644 (file)
@@ -3,12 +3,38 @@
 // http://jquery.com/docs/ajax/
 
 /**
- * Load HTML from a remote file and inject it into the DOM
+ * Load HTML from a remote file and inject it into the DOM, only if it's
+ * been modified by the server.
+ *
+ * @example $("#feeds").loadIfModified("feeds.html")
+ * @before <div id="feeds"></div>
+ * @result <div id="feeds"><b>45</b> feeds found.</div>
+ *
+ * @name loadIfModified
+ * @type jQuery
+ * @param String url The URL of the HTML file to load.
+ * @param Hash params A set of key/value pairs that will be sent to the server.
+ * @param Function callback A function to be executed whenever the data is loaded.
+ * @cat AJAX
  */
 jQuery.fn.loadIfModified = function( url, params, callback ) {
        this.load( url, params, callback, 1 );
 };
 
+/**
+ * Load HTML from a remote file and inject it into the DOM.
+ *
+ * @example $("#feeds").load("feeds.html")
+ * @before <div id="feeds"></div>
+ * @result <div id="feeds"><b>45</b> feeds found.</div>
+ *
+ * @name load
+ * @type jQuery
+ * @param String url The URL of the HTML file to load.
+ * @param Hash params A set of key/value pairs that will be sent to the server.
+ * @param Function callback A function to be executed whenever the data is loaded.
+ * @cat AJAX
+ */
 jQuery.fn.load = function( url, params, callback, ifModified ) {
        if ( url.constructor == Function )
                return this.bind("load", url);
@@ -58,7 +84,7 @@ jQuery.fn.load = function( url, params, callback, ifModified ) {
 };
 
 // If IE is used, create a wrapper for the XMLHttpRequest object
-if ( jQuery.browser.msie )
+if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
        XMLHttpRequest = function(){
                return new ActiveXObject(
                        navigator.userAgent.indexOf("MSIE 5") >= 0 ?
@@ -81,7 +107,56 @@ new function(){
 jQuery.extend({
 
        /**
-        * Load a remote page using a GET request
+        * Load a remote page using an HTTP GET request.
+        *
+        * @example $.get("test.cgi")
+        *
+        * @name $.get
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @cat AJAX
+        */
+        
+       /**
+        * Load a remote page using an HTTP GET request.
+        *
+        * @example $.get("test.cgi", { name: "John", time: "2pm" } )
+        *
+        * @name $.get
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Hash params A set of key/value pairs that will be sent to the server.
+        * @cat AJAX
+        */
+        
+       /**
+        * Load a remote page using an HTTP GET request.
+        *
+        * @example $.get("test.cgi", function(){
+        *   alert("Data Loaded.");
+        * })
+        *
+        * @name $.get
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Function callback A function to be executed whenever the data is loaded.
+        * @cat AJAX
+        */
+
+       /**
+        * Load a remote page using an HTTP GET request.
+        *
+        * @example $.get("test.cgi",
+        *   { name: "John", time: "2pm" },
+        *   function(){ alert("Data Loaded."); }
+        * )
+        *
+        * @name $.get
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Hash params A set of key/value pairs that will be sent to the server.
+        * @param Function callback A function to be executed whenever the data is loaded.
+        * @cat AJAX
         */
        get: function( url, data, callback, type, ifModified ) {
                if ( data.constructor == Function ) {
@@ -97,7 +172,63 @@ jQuery.extend({
                        if ( callback ) callback( jQuery.httpData(r,type), status );
                }, ifModified);
        },
+       
+       /**
+        * Load a remote page using an HTTP GET request, and only if it hasn't
+        * been modified since it was last retieved.
+        *
+        * @example $.getIfModified("test.cgi")
+        *
+        * @name $.getIfModified
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @cat AJAX
+        */
+        
+       /**
+        * Load a remote page using an HTTP GET request, and only if it hasn't
+        * been modified since it was last retieved.
+        *
+        * @example $.getIfModified("test.cgi", { name: "John", time: "2pm" })
+        *
+        * @name $.getIfModified
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Hash params A set of key/value pairs that will be sent to the server.
+        * @cat AJAX
+        */
+        
+       /**
+        * Load a remote page using an HTTP GET request, and only if it hasn't
+        * been modified since it was last retieved.
+        *
+        * @example $.getIfModified("test.cgi", function(){
+        *   alert("Data Loaded.");
+        * })
+        *
+        * @name $.getIfModified
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Function callback A function to be executed whenever the data is loaded.
+        * @cat AJAX
+        */
 
+       /**
+        * Load a remote page using an HTTP GET request, and only if it hasn't
+        * been modified since it was last retieved.
+        *
+        * @example $.getIfModified("test.cgi",
+        *   { name: "John", time: "2pm" },
+        *   function(){ alert("Data Loaded."); }
+        * )
+        *
+        * @name $.getIfModified
+        * @type jQuery
+        * @param String url The URL of the HTML file to load.
+        * @param Hash params A set of key/value pairs that will be sent to the server.
+        * @param Function callback A function to be executed whenever the data is loaded.
+        * @cat AJAX
+        */
        getIfModified: function( url, data, callback, type ) {
                jQuery.get(url, data, callback, type, 1);
        },
@@ -144,6 +275,8 @@ jQuery.extend({
                // Watch for a new set of requests
                if ( ! jQuery.active++ )
                        jQuery.event.trigger( "ajaxStart" );
+
+               var requestDone = false;
        
                // Create the request object
                var xml = new XMLHttpRequest();
@@ -170,8 +303,10 @@ jQuery.extend({
                // Wait for a response to come back
                var onreadystatechange = function(istimeout){
                        // The transfer is complete and the data is available, or the request timed out
-                       if ( xml && (xml.readyState == 4 || istimeout) ) {
-                               var status = jQuery.httpSuccess( xml ) && !istimeout ?
+                       if ( xml && (xml.readyState == 4 || istimeout == "timeout") ) {
+                               requestDone = true;
+
+                               var status = jQuery.httpSuccess( xml ) && istimeout != "timeout" ?
                                        ifModified && jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
                                
                                // Make sure that the request was successful or notmodified
@@ -221,8 +356,7 @@ jQuery.extend({
                                        // Cancel the request
                                        xml.abort();
 
-                                       // for Opera. Opera does't call onreadystatechange when aborted.
-                                       if (xml) onreadystatechange(1);
+                                       if ( !requestDone ) onreadystatechange( "timeout" );
 
                                        // Clear from memory
                                        xml = null;
@@ -239,9 +373,9 @@ jQuery.extend({
        // Determines if an XMLHttpRequest was successful or not
        httpSuccess: function(r) {
                try {
-                       return r.status ?
-                               ( r.status >= 200 && r.status < 300 ) || r.status == 304 :
-                               location.protocol == "file:";
+                       return !r.status && location.protocol == "file:" ||
+                               ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||
+                               jQuery.browser.safari && r.status == undefined;
                } catch(e){}
 
                return false;
@@ -253,7 +387,8 @@ jQuery.extend({
                        var xmlRes = xml.getResponseHeader("Last-Modified");
 
                        // Firefox always returns 200. check Last-Modified date
-                       if( xml.status == 304 || xmlRes == jQuery.lastModified[url]  ) return true;
+                       return xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
+                               jQuery.browser.safari && xml.status == undefined;
                } catch(e){}
 
                return false;
@@ -270,6 +405,9 @@ jQuery.extend({
                // If the type is "script", eval it
                if ( type == "script" ) eval.call( window, data );
 
+               // Get the JavaScript object, if JSON is used.
+               if ( type == "json" ) eval( "data = " + data );
+
                return data;
        },