Fixes #8138. Access to document.location is made only once at load time and if it fails (throwing an exception in IE when document.domain is already set), we use the href of an A element instead.

This commit is contained in:
jaubourg 2011-02-02 20:52:26 +01:00
parent b90369e8cb
commit e3cc440934

View file

@ -34,7 +34,22 @@ var r20 = /%20/g,
* 2) the catchall symbol "*" can be used * 2) the catchall symbol "*" can be used
* 3) selection will start with transport dataType and THEN go to "*" if needed * 3) selection will start with transport dataType and THEN go to "*" if needed
*/ */
transports = {}; transports = {},
// Stored document location array
ajaxLocation;
// #8138, IE may throw an exception when accessing
// a field from document.location if document.domain has been set
try {
ajaxLocation = document.location.href;
} catch( e ) {
// Use the href attribute of an A element
// since IE will modify it given document.location
ajaxLocation = document.createElement( "a" );
ajaxLocation.href = "";
ajaxLocation = ajaxLocation.href;
}
// Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
function addToPrefiltersOrTransports( structure ) { function addToPrefiltersOrTransports( structure ) {
@ -260,7 +275,7 @@ jQuery.extend({
}, },
ajaxSettings: { ajaxSettings: {
url: location.href, url: ajaxLocation,
global: true, global: true,
type: "GET", type: "GET",
contentType: "application/x-www-form-urlencoded", contentType: "application/x-www-form-urlencoded",
@ -361,8 +376,6 @@ jQuery.extend({
// timeout handle // timeout handle
timeoutTimer, timeoutTimer,
// Cross-domain detection vars // Cross-domain detection vars
loc = document.location,
protocol = loc.protocol || "http:",
parts, parts,
// The jqXHR state // The jqXHR state
state = 0, state = 0,
@ -549,7 +562,7 @@ jQuery.extend({
// Remove hash character (#7531: and string promotion) // Remove hash character (#7531: and string promotion)
// Add protocol if not provided (#5866: IE7 issue with protocol-less urls) // Add protocol if not provided (#5866: IE7 issue with protocol-less urls)
// We also use the url parameter if available // We also use the url parameter if available
s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ).replace( rprotocol, protocol + "//" ); s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ).replace( rprotocol, ajaxLocation[ 1 ] + "//" );
// Extract dataTypes list // Extract dataTypes list
s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax ); s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax );
@ -558,9 +571,9 @@ jQuery.extend({
if ( !s.crossDomain ) { if ( !s.crossDomain ) {
parts = rurl.exec( s.url.toLowerCase() ); parts = rurl.exec( s.url.toLowerCase() );
s.crossDomain = !!( parts && s.crossDomain = !!( parts &&
( parts[ 1 ] != protocol || parts[ 2 ] != loc.hostname || ( parts[ 1 ] != ajaxLocation[ 1 ] || parts[ 2 ] != ajaxLocation[ 2 ] ||
( parts[ 3 ] || ( parts[ 1 ] === "http:" ? 80 : 443 ) ) != ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? 80 : 443 ) ) !=
( loc.port || ( protocol === "http:" ? 80 : 443 ) ) ) ( ajaxLocation[ 3 ] || ( ajaxLocation[ 1 ] === "http:" ? 80 : 443 ) ) )
); );
} }
@ -717,6 +730,9 @@ jQuery.extend({
} }
}); });
// Segment ajaxLocation into parts
ajaxLocation = rurl.exec( ajaxLocation.toLowerCase() );
function buildParams( prefix, obj, traditional, add ) { function buildParams( prefix, obj, traditional, add ) {
if ( jQuery.isArray( obj ) && obj.length ) { if ( jQuery.isArray( obj ) && obj.length ) {
// Serialize array item. // Serialize array item.