Core: Use named exports in `src/`
[jquery.git] / src / ajax / script.js
blob3c88c1af305f4b283bf95b3733039d03c546ca68
1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.js";
4 import "../ajax.js";
6 function canUseScriptTag( s ) {
8         // A script tag can only be used for async, cross domain or forced-by-attrs requests.
9         // Requests with headers cannot use a script tag. However, when both `scriptAttrs` &
10         // `headers` options are specified, both are impossible to satisfy together; we
11         // prefer `scriptAttrs` then.
12         // Sync requests remain handled differently to preserve strict script ordering.
13         return s.scriptAttrs || (
14                 !s.headers &&
15                 (
16                         s.crossDomain ||
18                         // When dealing with JSONP (`s.dataTypes` include "json" then)
19                         // don't use a script tag so that error responses still may have
20                         // `responseJSON` set. Continue using a script tag for JSONP requests that:
21                         //   * are cross-domain as AJAX requests won't work without a CORS setup
22                         //   * have `scriptAttrs` set as that's a script-only functionality
23                         // Note that this means JSONP requests violate strict CSP script-src settings.
24                         // A proper solution is to migrate from using JSONP to a CORS setup.
25                         ( s.async && jQuery.inArray( "json", s.dataTypes ) < 0 )
26                 )
27         );
30 // Install script dataType. Don't specify `contents.script` so that an explicit
31 // `dataType: "script"` is required (see gh-2432, gh-4822)
32 jQuery.ajaxSetup( {
33         accepts: {
34                 script: "text/javascript, application/javascript, " +
35                         "application/ecmascript, application/x-ecmascript"
36         },
37         converters: {
38                 "text script": function( text ) {
39                         jQuery.globalEval( text );
40                         return text;
41                 }
42         }
43 } );
45 // Handle cache's special case and crossDomain
46 jQuery.ajaxPrefilter( "script", function( s ) {
47         if ( s.cache === undefined ) {
48                 s.cache = false;
49         }
51         // These types of requests are handled via a script tag
52         // so force their methods to GET.
53         if ( canUseScriptTag( s ) ) {
54                 s.type = "GET";
55         }
56 } );
58 // Bind script tag hack transport
59 jQuery.ajaxTransport( "script", function( s ) {
60         if ( canUseScriptTag( s ) ) {
61                 var script, callback;
62                 return {
63                         send: function( _, complete ) {
64                                 script = jQuery( "<script>" )
65                                         .attr( s.scriptAttrs || {} )
66                                         .prop( { charset: s.scriptCharset, src: s.url } )
67                                         .on( "load error", callback = function( evt ) {
68                                                 script.remove();
69                                                 callback = null;
70                                                 if ( evt ) {
71                                                         complete( evt.type === "error" ? 404 : 200, evt.type );
72                                                 }
73                                         } );
75                                 // Use native DOM manipulation to avoid our domManip AJAX trickery
76                                 document.head.appendChild( script[ 0 ] );
77                         },
78                         abort: function() {
79                                 if ( callback ) {
80                                         callback();
81                                 }
82                         }
83                 };
84         }
85 } );