blob: 410c82cab57bf50e5ee57118f29684d8de1c6bf5 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001define( [
2 "../core",
3 "../var/document",
4 "../ajax"
5], function( jQuery, document ) {
6
7"use strict";
8
9// Prevent auto-execution of scripts when no explicit dataType was provided (See gh-2432)
10jQuery.ajaxPrefilter( function( s ) {
11 if ( s.crossDomain ) {
12 s.contents.script = false;
13 }
14} );
15
16// Install script dataType
17jQuery.ajaxSetup( {
18 accepts: {
19 script: "text/javascript, application/javascript, " +
20 "application/ecmascript, application/x-ecmascript"
21 },
22 contents: {
23 script: /\b(?:java|ecma)script\b/
24 },
25 converters: {
26 "text script": function( text ) {
27 jQuery.globalEval( text );
28 return text;
29 }
30 }
31} );
32
33// Handle cache's special case and crossDomain
34jQuery.ajaxPrefilter( "script", function( s ) {
35 if ( s.cache === undefined ) {
36 s.cache = false;
37 }
38 if ( s.crossDomain ) {
39 s.type = "GET";
40 }
41} );
42
43// Bind script tag hack transport
44jQuery.ajaxTransport( "script", function( s ) {
45
46 // This transport only deals with cross domain or forced-by-attrs requests
47 if ( s.crossDomain || s.scriptAttrs ) {
48 var script, callback;
49 return {
50 send: function( _, complete ) {
51 script = jQuery( "<script>" )
52 .attr( s.scriptAttrs || {} )
53 .prop( { charset: s.scriptCharset, src: s.url } )
54 .on( "load error", callback = function( evt ) {
55 script.remove();
56 callback = null;
57 if ( evt ) {
58 complete( evt.type === "error" ? 404 : 200, evt.type );
59 }
60 } );
61
62 // Use native DOM manipulation to avoid our domManip AJAX trickery
63 document.head.appendChild( script[ 0 ] );
64 },
65 abort: function() {
66 if ( callback ) {
67 callback();
68 }
69 }
70 };
71 }
72} );
73
74} );