Fixes #8098. Use the fast document.head when available. Don't set unneeded "script...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 var // Next active xhr id
4         xhrId = jQuery.now(),
5
6         // active xhrs
7         xhrs = {},
8
9         // #5280: see below
10         xhrUnloadAbortInstalled,
11
12         // XHR used to determine supports properties
13         testXHR;
14
15 // Create the request object
16 // (This is still attached to ajaxSettings for backward compatibility)
17 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
18         /* Microsoft failed to properly
19          * implement the XMLHttpRequest in IE7 (can't request local files),
20          * so we use the ActiveXObject when it is available
21          * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
22          * we need a fallback.
23          */
24         function() {
25                 if ( window.location.protocol !== "file:" ) {
26                         try {
27                                 return new window.XMLHttpRequest();
28                         } catch( xhrError ) {}
29                 }
30
31                 try {
32                         return new window.ActiveXObject("Microsoft.XMLHTTP");
33                 } catch( activeError ) {}
34         } :
35         // For all other browsers, use the standard XMLHttpRequest object
36         function() {
37                 return new window.XMLHttpRequest();
38         };
39
40 // Test if we can create an xhr object
41 try {
42         testXHR = jQuery.ajaxSettings.xhr();
43 } catch( xhrCreationException ) {}
44
45 //Does this browser support XHR requests?
46 jQuery.support.ajax = !!testXHR;
47
48 // Does this browser support crossDomain XHR requests
49 jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
50
51 // No need for the temporary xhr anymore
52 testXHR = undefined;
53
54 // Create transport if the browser can provide an xhr
55 if ( jQuery.support.ajax ) {
56
57         jQuery.ajaxTransport(function( s ) {
58                 // Cross domain only allowed if supported through XMLHttpRequest
59                 if ( !s.crossDomain || jQuery.support.cors ) {
60
61                         var callback;
62
63                         return {
64                                 send: function( headers, complete ) {
65
66                                         // #5280: we need to abort on unload or IE will keep connections alive
67                                         if ( !xhrUnloadAbortInstalled ) {
68
69                                                 xhrUnloadAbortInstalled = 1;
70
71                                                 jQuery(window).bind( "unload", function() {
72
73                                                         // Abort all pending requests
74                                                         jQuery.each( xhrs, function( _, xhr ) {
75                                                                 if ( xhr.onreadystatechange ) {
76                                                                         xhr.onreadystatechange( 1 );
77                                                                 }
78                                                         } );
79
80                                                 } );
81                                         }
82
83                                         // Get a new xhr
84                                         var xhr = s.xhr(),
85                                                 handle;
86
87                                         // Open the socket
88                                         // Passing null username, generates a login popup on Opera (#2865)
89                                         if ( s.username ) {
90                                                 xhr.open( s.type, s.url, s.async, s.username, s.password );
91                                         } else {
92                                                 xhr.open( s.type, s.url, s.async );
93                                         }
94
95                                         // Requested-With header
96                                         // Not set for crossDomain requests with no content
97                                         // (see why at http://trac.dojotoolkit.org/ticket/9486)
98                                         // Won't change header if already provided
99                                         if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
100                                                 headers[ "x-requested-with" ] = "XMLHttpRequest";
101                                         }
102
103                                         // Need an extra try/catch for cross domain requests in Firefox 3
104                                         try {
105                                                 jQuery.each( headers, function( key, value ) {
106                                                         xhr.setRequestHeader( key, value );
107                                                 } );
108                                         } catch( _ ) {}
109
110                                         // Do send the request
111                                         // This may raise an exception which is actually
112                                         // handled in jQuery.ajax (so no try/catch here)
113                                         xhr.send( ( s.hasContent && s.data ) || null );
114
115                                         // Listener
116                                         callback = function( _, isAbort ) {
117
118                                                 // Was never called and is aborted or complete
119                                                 if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
120
121                                                         // Only called once
122                                                         callback = 0;
123
124                                                         // Do not keep as active anymore
125                                                         if ( handle ) {
126                                                                 xhr.onreadystatechange = jQuery.noop;
127                                                                 delete xhrs[ handle ];
128                                                         }
129
130                                                         // If it's an abort
131                                                         if ( isAbort ) {
132                                                                 // Abort it manually if needed
133                                                                 if ( xhr.readyState !== 4 ) {
134                                                                         xhr.abort();
135                                                                 }
136                                                         } else {
137                                                                 // Get info
138                                                                 var status = xhr.status,
139                                                                         statusText,
140                                                                         responseHeaders = xhr.getAllResponseHeaders(),
141                                                                         responses = {},
142                                                                         xml = xhr.responseXML;
143
144                                                                 // Construct response list
145                                                                 if ( xml && xml.documentElement /* #4958 */ ) {
146                                                                         responses.xml = xml;
147                                                                 }
148                                                                 responses.text = xhr.responseText;
149
150                                                                 // Firefox throws an exception when accessing
151                                                                 // statusText for faulty cross-domain requests
152                                                                 try {
153                                                                         statusText = xhr.statusText;
154                                                                 } catch( e ) {
155                                                                         // We normalize with Webkit giving an empty statusText
156                                                                         statusText = "";
157                                                                 }
158
159                                                                 // Filter status for non standard behaviors
160                                                                 status =
161                                                                         // Most browsers return 0 when it should be 200 for local files
162                                                                         // Opera returns 0 when it should be 304
163                                                                         // Webkit returns 0 for failing cross-domain no matter the real status
164                                                                         !status ?
165                                                                                 // All: for local files, 0 is a success
166                                                                                 ( location.protocol === "file:" ? 200 : (
167                                                                                         // Webkit, Firefox: filter out faulty cross-domain requests
168                                                                                         !s.crossDomain || statusText ?
169                                                                                         (
170                                                                                                 // Opera: filter out real aborts #6060
171                                                                                                 responseHeaders ?
172                                                                                                 304 :
173                                                                                                 0
174                                                                                         ) :
175                                                                                         // We assume 302 but could be anything cross-domain related
176                                                                                         302
177                                                                                 ) ) :
178                                                                                 (
179                                                                                         // IE sometimes returns 1223 when it should be 204 (see #1450)
180                                                                                         status == 1223 ?
181                                                                                                 204 :
182                                                                                                 status
183                                                                                 );
184
185                                                                 // Call complete
186                                                                 complete( status, statusText, responses, responseHeaders );
187                                                         }
188                                                 }
189                                         };
190
191                                         // if we're in sync mode or it's in cache
192                                         // and has been retrieved directly (IE6 & IE7)
193                                         // we need to manually fire the callback
194                                         if ( !s.async || xhr.readyState === 4 ) {
195                                                 callback();
196                                         } else {
197                                                 // Add to list of active xhrs
198                                                 handle = xhrId++;
199                                                 xhrs[ handle ] = xhr;
200                                                 xhr.onreadystatechange = callback;
201                                         }
202                                 },
203
204                                 abort: function() {
205                                         if ( callback ) {
206                                                 callback(0,1);
207                                         }
208                                 }
209                         };
210                 }
211         });
212 }
213
214 })( jQuery );