4f55dc5ad802aea873db81fcdd5b74300e9bc581
[jquery.git] / src / transports / script.js
1 (function( jQuery ) {
2
3 // Install text to script executor
4 jQuery.extend( true, jQuery.ajaxSettings , {
5
6         accepts: {
7                 script: "text/javascript, application/javascript"
8         },
9         
10         autoDataType: {
11                 script: /javascript/
12         },
13                 
14         dataConverters: {
15                 "text script": jQuery.globalEval
16         }
17 } );
18
19 // Bind script tag hack transport
20 jQuery.xhr.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( _ , statusText) {
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                                                 script = 0;
63                                                 
64                                                 // Callback
65                                                 callback( statusText ? 0 : 200, statusText || "success" );
66                                         }
67                                 };
68                                 // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
69                                 // This arises when a base node is used (#2709 and #4378).
70                                 head.insertBefore( script, head.firstChild );
71                         },
72                         
73                         abort: function(statusText) {
74                                 if ( script ) {
75                                         script.onload( 0 , statusText );
76                                 }
77                         }
78                 };
79         }
80 });
81
82 })( jQuery );