Fixes #2994. Not finding a transport now fires the error callbacks and doesn't make...
[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 // Handle cache's special case and global
19 }).ajaxPrefilter("script", function(s) {
20
21         if ( s.cache === undefined ) {
22                 s.cache = false;
23         }
24
25         if ( s.crossDomain ) {
26                 s.global = false;
27         }
28
29 // Bind script tag hack transport
30 }).ajaxTransport("script", function(s) {
31
32         // This transport only deals with cross domain requests
33         if ( s.crossDomain ) {
34
35                 var script,
36                         head = document.getElementsByTagName("head")[0] || document.documentElement;
37
38                 return {
39
40                         send: function(_, callback) {
41
42                                 script = document.createElement("script");
43
44                                 script.async = "async";
45
46                                 if ( s.scriptCharset ) {
47                                         script.charset = s.scriptCharset;
48                                 }
49
50                                 script.src = s.url;
51
52                                 // Attach handlers for all browsers
53                                 script.onload = script.onreadystatechange = function( _ , isAbort ) {
54
55                                         if ( ! script.readyState || /loaded|complete/.test( script.readyState ) ) {
56
57                                                 // Handle memory leak in IE
58                                                 script.onload = script.onreadystatechange = null;
59
60                                                 // Remove the script
61                                                 if ( head && script.parentNode ) {
62                                                         head.removeChild( script );
63                                                 }
64
65                                                 // Dereference the script
66                                                 script = 0;
67
68                                                 // Callback if not abort
69                                                 if ( ! isAbort ) {
70                                                         callback( 200, "success" );
71                                                 }
72                                         }
73                                 };
74                                 // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
75                                 // This arises when a base node is used (#2709 and #4378).
76                                 head.insertBefore( script, head.firstChild );
77                         },
78
79                         abort: function() {
80                                 if ( script ) {
81                                         script.onload(0,1);
82                                 }
83                         }
84                 };
85         }
86 });
87
88 })( jQuery );