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