ajax: script: Threat sameDomain file:// requests as crossDomain
[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 primarily deals with cross domain requests
34         // but also sameDomain request within file:// due to
35         // http://code.google.com/p/chromium/issues/detail?id=4197 + 47416
36         var local = location.protocol === 'file:';
37         if ( s.crossDomain || local ) {
38
39                 var script,
40                         head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
41
42                 return {
43
44                         send: function( _, callback ) {
45
46                                 script = document.createElement( "script" );
47
48                                 script.async = "async";
49
50                                 if ( s.scriptCharset ) {
51                                         script.charset = s.scriptCharset;
52                                 }
53
54                                 script.src = s.url;
55
56                                 // Attach handlers for all browsers
57                                 script.onload = script.onreadystatechange = function( _, isAbort ) {
58
59                                         if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
60
61                                                 // Handle memory leak in IE
62                                                 script.onload = script.onreadystatechange = null;
63
64                                                 // Remove the script
65                                                 if ( head && script.parentNode ) {
66                                                         head.removeChild( script );
67                                                 }
68
69                                                 // Dereference the script
70                                                 script = undefined;
71
72                                                 // Callback if not abort
73                                                 if ( !isAbort ) {
74                                                         callback( 200, "success" );
75                                                 }
76                                         }
77                                 };
78                                 // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
79                                 // This arises when a base node is used (#2709 and #4378).
80                                 head.insertBefore( script, head.firstChild );
81                         },
82
83                         abort: function() {
84                                 if ( script ) {
85                                         script.onload( 0, 1 );
86                                 }
87                         }
88                 };
89         }
90 } );
91
92 })( jQuery );