aboutsummaryrefslogtreecommitdiffstats
path: root/src/ajax/script.js
blob: 0db0de6c7ef7d56dfc4047018028efd870a249a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function( jQuery ) {

// Install text to script executor
jQuery.extend( true, jQuery.ajaxSettings , {

	accepts: {
		script: "text/javascript, application/javascript"
	},

	contents: {
		script: /javascript/
	},

	converters: {
		"text script": jQuery.globalEval
	}
} );

// Bind script tag hack transport
jQuery.ajax.transport("script", function(s) {

	// Handle cache special case
	if ( s.cache === undefined ) {
		s.cache = false;
	}

	// This transport only deals with cross domain get requests
	if ( s.crossDomain && s.async && ( s.type === "GET" || ! s.data ) ) {

		s.global = false;

		var script,
			head = document.getElementsByTagName("head")[0] || document.documentElement;

		return {

			send: function(_, callback) {

				script = document.createElement("script");

				script.async = "async";

				if ( s.scriptCharset ) {
					script.charset = s.scriptCharset;
				}

				script.src = s.url;

				// Attach handlers for all browsers
				script.onload = script.onreadystatechange = function( _ , statusText) {

					if ( ! script.readyState || /loaded|complete/.test( script.readyState ) ) {

						// Handle memory leak in IE
						script.onload = script.onreadystatechange = null;

						// Remove the script
						if ( head && script.parentNode ) {
							head.removeChild( script );
						}

						script = 0;

						// Callback
						callback( statusText ? 0 : 200, statusText || "success" );
					}
				};
				// Use insertBefore instead of appendChild  to circumvent an IE6 bug.
				// This arises when a base node is used (#2709 and #4378).
				head.insertBefore( script, head.firstChild );
			},

			abort: function(statusText) {
				if ( script ) {
					script.onload( 0 , statusText );
				}
			}
		};
	}
});

})( jQuery );