Skip to content

Commit

Permalink
Make sure that the ActiveX exception is caught if it's unable to be l…
Browse files Browse the repository at this point in the history
…oaded. Fixes #2849.
  • Loading branch information
jeresig committed Jan 5, 2010
1 parent b2289f3 commit 3f648c4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/ajax.js
Expand Up @@ -179,9 +179,14 @@ jQuery.extend({
// so we use the ActiveXObject when it is available
// This function can be overriden by calling jQuery.ajaxSetup
xhr: function() {
return window.XMLHttpRequest && window.location.protocol !== "file:" || window.ActiveXObject ?
new window.XMLHttpRequest() :
new window.ActiveXObject("Microsoft.XMLHTTP");
if ( window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ) {
return new window.XMLHttpRequest();

} else {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
},
accepts: {
xml: "application/xml, text/xml",
Expand Down Expand Up @@ -326,6 +331,10 @@ jQuery.extend({
// Create the request object
var xhr = s.xhr();

if ( !xhr ) {
return;
}

// Open the socket
// Passing null username, generates a login popup on Opera (#2865)
if ( s.username ) {
Expand Down

7 comments on commit 3f648c4

@GarrettS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is using file protocol?

Have you considered creating an identifier for native support in containing scope?

var IS_NATIVE_XHR = typeof XMLHttpRequest != "undefined";

@kangax
Copy link

@kangax kangax commented on 3f648c4 Jan 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just declare this whole function conditionally, for best performance.

@jdalton
Copy link
Member

@jdalton jdalton commented on 3f648c4 Jan 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garrett IE7+ can't access local files from XMLHttpRequest and has to use ActiveXObject

@jeresig
Copy link
Member Author

@jeresig jeresig commented on 3f648c4 Jan 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just split the function in two: c68fbc2

@GarrettS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdalton;

Yes, I know XMLHttpRequest doesn't support file:..

I am curios as what sort of application is using file protocol. As in "why do you need that?".

I just committed some XHR stuff, to APE.

@GarrettS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant in IE. XMLHttpRequest doesn't support file in IE.

Other implementations may, but that is not something that should be relied upon; it is an implementation-dependent feature. So I am wondering who is relying on that nonstandard behavior for production code and why it is there. I know the history with jQUery and wanting to support file, but not the reason for wanting.

http://www.w3.org/TR/XMLHttpRequest/

Second, it can be used to make requests over both HTTP and HTTPS (some implementations support protocols in addition to HTTP and HTTPS, but that functionality is not covered by this specification)

@SamWM
Copy link

@SamWM SamWM commented on 3f648c4 Jan 8, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be cases where jQuery AJAX may be used on file is serverless intranets or for testing without a web server, i.e. accessed via I:\intranet\index.html for example. Probably rarely used by developers, but is there any harm in including it? MooTools will fail with file (http://github.com/mootools/mootools-core/blob/master/Source/Core/Browser.js#L70) as will Prototype (http://github.com/sstephenson/prototype/blob/master/src/ajax/ajax.js).

There is also the possibility that jQuery may be used in HTML Applications (HTA) or Windows gadgets (although I do not know if people use it or how well it works). Sticking to the standards is important, but why not go further (as long as it doesn't break other browsers or involve too much extra code)?

Please sign in to comment.