Skip to main content

Bug Tracker

Side navigation

Ticket #1768: 1768.diff


File 1768.diff, 2.6 KB (added by davidserduke, December 14, 2007 08:46PM UTC)

patch (not complete)

Index: src/ajax.js
===================================================================
--- src/ajax.js	(revision 4156)
+++ src/ajax.js	(working copy)
@@ -184,14 +184,18 @@
 			// that a JSONP style response is executed properly
 			s.dataType = "script";
 
+			function jsonpEnd() {
+				// Garbage collect
+				window[ jsonp ] = undefined;
+				try{ delete window[ jsonp ]; } catch(e){}
+			}
+
 			// Handle JSONP-style loading
 			window[ jsonp ] = function(tmp){
 				data = tmp;
 				success();
 				complete();
-				// Garbage collect
-				window[ jsonp ] = undefined;
-				try{ delete window[ jsonp ]; } catch(e){}
+				jsonpEnd();
 			};
 		}
 
@@ -221,26 +225,45 @@
 		// If we're requesting a remote document
 		// and trying to load JSON or Script with a GET
 		if ( (!s.url.indexOf("http") || !s.url.indexOf("//")) && ( s.dataType == "script" || s.dataType =="json" ) && s.type.toLowerCase() == "get" ) {
-			var head = document.getElementsByTagName("head")[0];
-			var script = document.createElement("script");
+			var head = document.getElementsByTagName("head")[0],
+				script = document.createElement("script"),
+				done = false;
 			script.src = s.url;
 
-			// Handle Script loading
-			if ( !jsonp ) {
-				var done = false;
-
-				// Attach handlers for all browsers
-				script.onload = script.onreadystatechange = function(){
-					if ( !done && (!this.readyState || 
-							this.readyState == "loaded" || this.readyState == "complete") ) {
-						done = true;
-						success();
+			function scriptTagEnd(how) {
+				if (!done) {
+					done = true;
+					status = how;
+					// don't do success/complete calls on jsonp success
+					if (!jsonp || status != "success") {
+						if (status == "success")
+							success();
+						else {
+							jQuery.handleError(s, null, status);
+							if (jsonp)
+								jsonpEnd();
+						}
 						complete();
-						head.removeChild( script );
 					}
-				};
+
+					head.removeChild( script );
+				}
 			}
 
+			// Attach handlers for all browsers
+			script.onload = script.onreadystatechange = function(){
+				if ( !done && (!this.readyState || 
+						this.readyState == "loaded" || this.readyState == "complete") )
+					// opera sets the .text value so we can test that, the other browsers don't so have assume success
+					scriptTagEnd(this.text || !jQuery.browser.opera ? "success" : "error");
+			};
+			script.onerror = function(){
+				scriptTagEnd("error");
+			};
+
+			if (s.timeout)
+				setTimeout(function(){ scriptTagEnd("timeout"); }, s.timeout);
+
 			head.appendChild(script);
 
 			// We handle everything using the script element injection

Download in other formats:

Original Format