Apply JQuery Core Style Guidelines to ajax.js and ajax/*.js,
[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"
7         },
8         contents: {
9                 script: /javascript/
10         },
11         converters: {
12                 "text script": jQuery.globalEval
13         }
14 });
15
16 // Handle cache's special case and global
17 jQuery.ajaxPrefilter( "script", function(s) {
18         if ( s.cache === undefined ) {
19                 s.cache = false;
20         }
21         if ( s.crossDomain ) {
22                 s.type = "GET";
23                 s.global = false;
24         }
25 } );
26
27 // Bind script tag hack transport
28 jQuery.ajaxTransport( "script", function(s) {
29
30         // This transport only deals with cross domain requests
31         if ( s.crossDomain ) {
32
33                 var script,
34                         head = document.getElementsByTagName( "head" )[ 0 ] || document.documentElement;
35
36                 return {
37
38                         send: function( _, callback ) {
39
40                                 script = document.createElement( "script" );
41
42                                 script.async = "async";
43
44                                 if ( s.scriptCharset ) {
45                                         script.charset = s.scriptCharset;
46                                 }
47
48                                 script.src = s.url;
49
50                                 // Attach handlers for all browsers
51                                 script.onload = script.onreadystatechange = function( _, isAbort ) {
52
53                                         if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
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                                                 // Dereference the script
64                                                 script = undefined;
65
66                                                 // Callback if not abort
67                                                 if ( !isAbort ) {
68                                                         callback( 200, "success" );
69                                                 }
70                                         }
71                                 };
72                                 // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
73                                 // This arises when a base node is used (#2709 and #4378).
74                                 head.insertBefore( script, head.firstChild );
75                         },
76
77                         abort: function() {
78                                 if ( script ) {
79                                         script.onload( 0, 1 );
80                                 }
81                         }
82                 };
83         }
84 } );
85
86 })( jQuery );