Moved unload abort code so that the event is only bound if the xhr transport is used...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 var // Next fake timer id
4         xhrPollingId = jQuery.now(),
5
6         // Callbacks hashtable
7         xhrs = {},
8
9         // XHR pool
10         xhrPool = [],
11
12         // #5280: see end of file
13         xhrUnloadAbortMarker;
14
15
16 jQuery.ajax.transport( function( s , determineDataType ) {
17
18         // Cross domain only allowed if supported through XMLHttpRequest
19         if ( ! s.crossDomain || jQuery.support.cors ) {
20
21                 var callback;
22
23                 return {
24
25                         send: function(headers, complete) {
26
27                                 // #5280: we need to abort on unload or IE will keep connections alive
28                                 if ( ! xhrUnloadAbortMarker ) {
29
30                                         xhrUnloadAbortMarker = [];
31
32                                         jQuery(window).bind( "unload" , function() {
33
34                                                 // Abort all pending requests
35                                                 jQuery.each(xhrs, function(_, xhr) {
36                                                         if ( xhr.onreadystatechange ) {
37                                                                 xhr.onreadystatechange( xhrUnloadAbortMarker );
38                                                         }
39                                                 });
40
41                                                 // Reset polling structure to be safe
42                                                 xhrs = {};
43
44                                         });
45                                 }
46
47                                 var xhr = xhrPool.pop() || s.xhr(),
48                                         handle;
49
50                                 // Open the socket
51                                 // Passing null username, generates a login popup on Opera (#2865)
52                                 if ( s.username ) {
53                                         xhr.open(s.type, s.url, s.async, s.username, s.password);
54                                 } else {
55                                         xhr.open(s.type, s.url, s.async);
56                                 }
57
58                                 // Requested-With header
59                                 // Not set for crossDomain requests with no content
60                                 // (see why at http://trac.dojotoolkit.org/ticket/9486)
61                                 // Won't change header if already provided in beforeSend
62                                 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
63                                         headers["x-requested-with"] = "XMLHttpRequest";
64                                 }
65
66                                 // Need an extra try/catch for cross domain requests in Firefox 3
67                                 try {
68
69                                         jQuery.each(headers, function(key,value) {
70                                                 xhr.setRequestHeader(key,value);
71                                         });
72
73                                 } catch(_) {}
74
75                                 // Do send the request
76                                 try {
77                                         xhr.send( ( s.hasContent && s.data ) || null );
78                                 } catch(e) {
79                                         // Store back in pool
80                                         xhrPool.push( xhr );
81                                         complete(0, "error", "" + e);
82                                         return;
83                                 }
84
85                                 // Listener
86                                 callback = function ( abortStatusText ) {
87
88                                         // Was never called and is aborted or complete
89                                         if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
90
91                                                 // Do not listen anymore
92                                                 // and Store back in pool
93                                                 if (handle) {
94                                                         xhr.onreadystatechange = jQuery.noop;
95                                                         delete xhrs[ handle ];
96                                                         handle = undefined;
97                                                         xhrPool.push( xhr );
98                                                 }
99
100                                                 callback = 0;
101
102                                                 // Get info
103                                                 var status, statusText, response, responseHeaders;
104
105                                                 if ( abortStatusText ) {
106
107                                                         if ( xhr.readyState !== 4 ) {
108                                                                 xhr.abort();
109                                                         }
110
111                                                         // Stop here if unloadAbort
112                                                         if ( abortStatusText === xhrUnloadAbortMarker ) {
113                                                                 return;
114                                                         }
115
116                                                         status = 0;
117                                                         statusText = abortStatusText;
118
119                                                 } else {
120
121                                                         status = xhr.status;
122
123                                                         try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
124
125                                                                 statusText = xhr.statusText;
126
127                                                         } catch( e ) {
128
129                                                                 statusText = ""; // We normalize with Webkit giving an empty statusText
130
131                                                         }
132
133                                                         responseHeaders = xhr.getAllResponseHeaders();
134
135                                                         // Filter status for non standard behaviours
136                                                         // (so many they seem to be the actual "standard")
137                                                         status =
138                                                                 // Opera returns 0 when it should be 304
139                                                                 // Webkit returns 0 for failing cross-domain no matter the real status
140                                                                 status === 0 ?
141                                                                         (
142                                                                                 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
143                                                                                 (
144                                                                                         responseHeaders ? // Opera: filter out real aborts #6060
145                                                                                         304
146                                                                                         :
147                                                                                         0
148                                                                                 )
149                                                                                 :
150                                                                                 302 // We assume 302 but could be anything cross-domain related
151                                                                         )
152                                                                         :
153                                                                         (
154                                                                                 status == 1223 ?        // IE sometimes returns 1223 when it should be 204 (see #1450)
155                                                                                         204
156                                                                                         :
157                                                                                         status
158                                                                         );
159
160                                                         // Guess response & update dataType accordingly
161                                                         response =
162                                                                 determineDataType(
163                                                                         s,
164                                                                         xhr.getResponseHeader("content-type"),
165                                                                         xhr.responseText,
166                                                                         xhr.responseXML );
167                                                 }
168
169                                                 // Call complete
170                                                 complete(status,statusText,response,responseHeaders);
171                                         }
172                                 };
173
174                                 // if we're in sync mode
175                                 // or it's in cache and has been retrieved directly (IE6 & IE7)
176                                 // we need to manually fire the callback
177                                 if ( ! s.async || xhr.readyState === 4 ) {
178
179                                         callback();
180
181                                 } else {
182
183                                         // Listener is externalized to handle abort on unload
184                                         handle = xhrPollingId++;
185                                         xhrs[ handle ] = xhr;
186                                         xhr.onreadystatechange = function() {
187                                                 callback();
188                                         };
189                                 }
190                         },
191
192                         abort: function(statusText) {
193                                 if ( callback ) {
194                                         callback(statusText);
195                                 }
196                         }
197                 };
198         }
199 });
200
201 })( jQuery );