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