1 import { jQuery } from "../core.js";
2 import { document } from "../var/document.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 || (
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 )
30 // Install script dataType. Don't specify `contents.script` so that an explicit
31 // `dataType: "script"` is required (see gh-2432, gh-4822)
34 script: "text/javascript, application/javascript, " +
35 "application/ecmascript, application/x-ecmascript"
38 "text script": function( text ) {
39 jQuery.globalEval( text );
45 // Handle cache's special case and crossDomain
46 jQuery.ajaxPrefilter( "script", function( s ) {
47 if ( s.cache === undefined ) {
51 // These types of requests are handled via a script tag
52 // so force their methods to GET.
53 if ( canUseScriptTag( s ) ) {
58 // Bind script tag hack transport
59 jQuery.ajaxTransport( "script", function( s ) {
60 if ( canUseScriptTag( s ) ) {
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 ) {
71 complete( evt.type === "error" ? 404 : 200, evt.type );
75 // Use native DOM manipulation to avoid our domManip AJAX trickery
76 document.head.appendChild( script[ 0 ] );