jquery/src/ajax/script.js

90 lines
1.9 KiB
JavaScript
Raw Normal View History

(function( jQuery ) {
// Install script dataType
jQuery.ajaxSetup({
accepts: {
script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"
},
contents: {
script: /javascript|ecmascript/
},
converters: {
"text script": function( text ) {
jQuery.globalEval( text );
return text;
}
}
});
// Handle cache's special case and global
2011-01-23 20:46:09 +01:00
jQuery.ajaxPrefilter( "script", function( s ) {
if ( s.cache === undefined ) {
s.cache = false;
}
if ( s.crossDomain ) {
s.type = "GET";
s.global = false;
}
});
// Bind script tag hack transport
jQuery.ajaxTransport( "script", function(s) {
// This transport only deals with cross domain requests
if ( s.crossDomain ) {
2011-01-05 22:41:23 +01:00
var script,
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
2011-01-05 22:41:23 +01:00
return {
2011-01-05 22:41:23 +01:00
send: function( _, callback ) {
script = document.createElement( "script" );
script.async = "async";
if ( s.scriptCharset ) {
script.charset = s.scriptCharset;
}
2011-01-05 22:41:23 +01:00
script.src = s.url;
2011-01-05 22:41:23 +01:00
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function( _, isAbort ) {
2011-01-05 22:41:23 +01:00
if ( isAbort || !script.readyState || /loaded|complete/.test( script.readyState ) ) {
2011-01-05 22:41:23 +01:00
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
2011-01-05 22:41:23 +01:00
// Remove the script
if ( head && script.parentNode ) {
head.removeChild( script );
}
2011-01-05 22:41:23 +01:00
// Dereference the script
script = undefined;
2011-01-05 22:41:23 +01:00
// Callback if not abort
if ( !isAbort ) {
callback( 200, "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 );
},
2011-01-05 22:41:23 +01:00
abort: function() {
if ( script ) {
script.onload( 0, 1 );
}
}
};
}
});
})( jQuery );