Made more formatting changes to manipulation.js. Also moved all inline RegExp into...
[jquery.git] / src / ajax.js
index 4db08a4..a41de4c 100644 (file)
@@ -170,6 +170,7 @@ jQuery.extend({
 
        // Last-Modified header cache for next request
        lastModified: {},
+       etag: {},
 
        ajax: function( s ) {
                // Extend the settings, but re-extend 's' so that it can be
@@ -186,15 +187,15 @@ jQuery.extend({
                // Handle JSONP Parameter Callbacks
                if ( s.dataType == "jsonp" ) {
                        if ( type == "GET" ) {
-                               if ( !s.url.match(jsre) )
-                                       s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?";
-                       } else if ( !s.data || !s.data.match(jsre) )
+                               if ( jsre.test( !s.url ) )
+                                       s.url += (/\?/.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
+                       } else if ( !s.data || !jsre.test(s.data) )
                                s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
                        s.dataType = "json";
                }
 
                // Build temporary JSONP function
-               if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) {
+               if ( s.dataType == "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
                        jsonp = "jsonp" + jsc++;
 
                        // Replace the =? sequence both in the query string and the data
@@ -227,12 +228,12 @@ jQuery.extend({
                        // try replacing _= if it is there
                        var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
                        // if nothing was replaced, add timestamp to the end
-                       s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
+                       s.url = ret + ((ret == s.url) ? (/\?/.test(s.url) ? "&" : "?") + "_=" + ts : "");
                }
 
                // If data is available, append data to url for get requests
                if ( s.data && type == "GET" ) {
-                       s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
+                       s.url += (/\?/.test(s.url) ? "&" : "?") + s.data;
                }
 
                // Watch for a new set of requests
@@ -298,10 +299,13 @@ jQuery.extend({
                        if ( s.data )
                                xhr.setRequestHeader("Content-Type", s.contentType);
 
-                       // Set the If-Modified-Since header, if ifModified mode.
-                       if ( s.ifModified )
-                               xhr.setRequestHeader("If-Modified-Since",
-                                       jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
+                               // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
+                               if ( s.ifModified ) {
+                                       if (jQuery.lastModified[s.url])
+                                               xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
+                                       if (jQuery.etag[s.url])
+                                               xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
+                               }
 
                        // Set header so the called script knows that it's an XMLHttpRequest
                        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
@@ -363,16 +367,7 @@ jQuery.extend({
                                }
 
                                // Make sure that the request was successful or notmodified
-                               if ( status == "success" ) {
-                                       // Cache Last-Modified header, if ifModified mode.
-                                       var modRes;
-                                       try {
-                                               modRes = xhr.getResponseHeader("Last-Modified");
-                                       } catch(e) {} // swallow exception thrown by FF if header is not available
-
-                                       if ( s.ifModified && modRes )
-                                               jQuery.lastModified[s.url] = modRes;
-
+                               if ( status == "success" || status == "notmodified" ) {
                                        // JSONP handles its own success callback
                                        if ( !jsonp )
                                                success();
@@ -460,20 +455,25 @@ jQuery.extend({
                try {
                        // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
                        return !xhr.status && location.protocol == "file:" ||
-                               ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223;
+                               // Opera returns 0 when status is 304
+                               ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || xhr.status == 0;
                } catch(e){}
                return false;
        },
 
        // Determines if an XMLHttpRequest returns NotModified
        httpNotModified: function( xhr, url ) {
-               try {
-                       var xhrRes = xhr.getResponseHeader("Last-Modified");
+               var last_modified = xhr.getResponseHeader("Last-Modified");
+               var etag = xhr.getResponseHeader("Etag");
 
-                       // Firefox always returns 200. check Last-Modified date
-                       return xhr.status == 304 || xhrRes == jQuery.lastModified[url];
-               } catch(e){}
-               return false;
+               if (last_modified) 
+                       jQuery.lastModified[url] = last_modified;
+
+               if (etag) 
+                       jQuery.etag[url] = etag;
+
+               // Opera returns 0 when status is 304
+               return xhr.status == 304 || xhr.status == 0;
        },
 
        httpData: function( xhr, type, s ) {
@@ -481,7 +481,7 @@ jQuery.extend({
                        xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
                        data = xml ? xhr.responseXML : xhr.responseText;
 
-               if ( xml && data.documentElement.tagName == "parsererror" ) {
+               if ( xml && data.documentElement.nodeName == "parsererror" ) {
                        throw "parsererror";
                }