The script prefilter now forces cross-domain requests type to GET.
[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.type = "GET";
27                 s.global = false;
28         }
29
30 // Bind script tag hack transport
31 }).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 = 0;
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 );