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