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