Reworked script and xhr abort logic to take advantage of the fact jXHR.abort will...
[jquery.git] / src / ajax / script.js
1 (function( jQuery ) {
2
3 // Install script dataType
4 jQuery.ajaxSetup({
5
6         accepts: {
7                 script: "text/javascript, application/javascript"
8         },
9
10         contents: {
11                 script: /javascript/
12         },
13
14         converters: {
15                 "text script": jQuery.globalEval
16         }
17 });
18
19 // Bind script tag hack transport
20 jQuery.ajax.transport("script", function(s) {
21
22         // Handle cache special case
23         if ( s.cache === undefined ) {
24                 s.cache = false;
25         }
26
27         // This transport only deals with cross domain get requests
28         if ( s.crossDomain && s.async && ( s.type === "GET" || ! s.data ) ) {
29
30                 s.global = false;
31
32                 var script,
33                         head = document.getElementsByTagName("head")[0] || document.documentElement;
34
35                 return {
36
37                         send: function(_, callback) {
38
39                                 script = document.createElement("script");
40
41                                 script.async = "async";
42
43                                 if ( s.scriptCharset ) {
44                                         script.charset = s.scriptCharset;
45                                 }
46
47                                 script.src = s.url;
48
49                                 // Attach handlers for all browsers
50                                 script.onload = script.onreadystatechange = function( _ , isAbort ) {
51
52                                         if ( ! script.readyState || /loaded|complete/.test( script.readyState ) ) {
53
54                                                 // Handle memory leak in IE
55                                                 script.onload = script.onreadystatechange = null;
56
57                                                 // Remove the script
58                                                 if ( head && script.parentNode ) {
59                                                         head.removeChild( script );
60                                                 }
61
62                                                 // Dereference the script
63                                                 script = 0;
64
65                                                 // Callback if not abort
66                                                 if ( ! isAbort ) {
67                                                         callback( 200, "success" );
68                                                 }
69                                         }
70                                 };
71                                 // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
72                                 // This arises when a base node is used (#2709 and #4378).
73                                 head.insertBefore( script, head.firstChild );
74                         },
75
76                         abort: function() {
77                                 if ( script ) {
78                                         script.onload(0,1);
79                                 }
80                         }
81                 };
82         }
83 });
84
85 })( jQuery );