Skip to content

Simplify XHR detection #1967

Description

@FarSeeing

This is the XMLHttpRequest object detection code:

https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js

jQuery.ajaxSettings.xhr = function() {
    try {
        return new XMLHttpRequest();
    } catch ( e ) {}
};
...
xhrSupported = jQuery.ajaxSettings.xhr();
...
support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
support.ajax = xhrSupported = !!xhrSupported;

support.ajax is used nowhere in the code and is not documented, XMLHttpRequest object is supported in all modern browsers (including Android 2.3, iOS 6.1, Opera 12 and IE 9 — caniuse.com).
Why not remove xhrSupported variable together with support.ajax and try-catch block?

Activity

  1. gibson042 commented on Dec 24, 2014

    @gibson042
    Member

    Sounds like a great idea; I only see references to it in test/unit/support.js and those can go away as well since we don't guarantee any jQuery.support properties. Would you like to file a PR?

  2. markelog commented on Dec 24, 2014

    @markelog
    Member

    XMLHttpRequest object is supported in all modern browsers

    Right, but you can disable it in IE, but i'd say we shouldn't support that anymore.

    try..catch was always weird me out here, it's understanble why it was used for activeX but I hear it's pointless.

    xhr method was never documented and tested so i'd say we should get rid of it too.

    Not sure about support.cors property, we never used it anywhere else, but it might be useful for tests, so i either remove it or add tests for it.

    /cc @jaubourg

  3. FarSeeing commented on Dec 26, 2014

    @FarSeeing
    ContributorAuthor

    Not sure about support.cors property, we never used it anywhere else, but it might be useful for tests, so i either remove it or add tests for it.

    withCredentials property has appeared only in IE10 (MSDN) so you cannot just drop that check.

  4. added 2 commits that reference this issue on Dec 26, 2014
    209560e
    8432ed5
  5. mgol commented on Dec 26, 2014

    @mgol
    Member

    @markelog

    XMLHttpRequest object is supported in all modern browsers

    Right, but you can disable it in IE, but i'd say we shouldn't support that anymore.

    I tend to agree, especially on master (I'd like to hear @dmethvin's thoughts on that, though).

    Not sure about support.cors property, we never used it anywhere else, but it might be useful for tests, so i either remove it or add tests for it.

    IMO we should keep it. Our detection code is not perfect in weird setups so it's nice if people can workaround potential problems just by setting jQuery.support.cors to true (even if we don't officially support it). See e.g. #1881.

  6. jaubourg commented on Dec 26, 2014

    @jaubourg
    Member

    XMLHttpRequest object is supported in all modern browsers

    Right, but you can disable it in IE, but i'd say we shouldn't support that anymore.

    I tend to agree, especially on master (I'd like to hear @dmethvin's thoughts on that, though).

    Hence the try/catch statement that prevents crashing at startup.

    Not sure about support.cors property, we never used it anywhere else, but it might be useful for tests, so i either remove it or add tests for it.

    IMO we should keep it. Our detection code is not perfect in weird setups so it's nice if people can workaround potential problems just by setting jQuery.support.cors to true (even if we don't officially support it). See e.g. #1881.

    We should definitely keep support.cors, meaning we need to create an xhr at startup, meaning we need the try/catch.

  7. mgol commented on Dec 26, 2014

    @mgol
    Member

    We should definitely keep support.cors, meaning we need to create an xhr at startup, meaning we need the try/catch.

    Does IE with native XHR disabled still have the XMLHttpRequest global but it just crashes? Otherwise it'd be better to change the code so that it's a noop or sth like that and not rely on try/catch.

  8. jaubourg commented on Dec 26, 2014

    @jaubourg
    Member

    (on a side note, we also need to think about those non-browser environments that may not have an xhr implementation into which we may not want to fail miserably)

    Does IE with native XHR disabled still have the XMLHttpRequest global but it just crashes? Otherwise it'd be better to change the code so that it's a noop or sth like that and not rely on try/catch.

    Whether it's undefined or throws an exception when the constructor is invoked is irrelevant since both cases are handled by the try/catch. It's pretty solid as is which was the point.

  9. mgol commented on Dec 26, 2014

    @mgol
    Member

    (on a side note, we also need to think about those non-browser environments that may not have an xhr implementation into which we may not want to fail miserably)

    Right; I'll land #1949 in a moment so that we get covered here.

  10. mgol commented on Dec 26, 2014

    @mgol
    Member

    Whether it's undefined or throws an exception when the constructor is invoked is irrelevant since both cases are handled by the try/catch.

    Right but if it never throwed then it'd be better to avoid try/catch. But I guess we don't have the guarantee.

  11. jaubourg commented on Dec 26, 2014

    @jaubourg
    Member

    But I guess we don't have the guarantee.

    Exactly. The module we're talking about being ajax, I tend to apply a "better safe than sorry" policy ;)

  12. FarSeeing commented on Dec 26, 2014

    @FarSeeing
    ContributorAuthor

    Does IE with native XHR disabled still have the XMLHttpRequest global but it just crashes?

    I've checked IE11 — you just cannot disable XHR at all. Will check IE9 and IE10 later.

  13. FarSeeing commented on Dec 26, 2014

    @FarSeeing
    ContributorAuthor

    Checked IE9: even when you disable XHR, window.XMLHttpRequest is still available.

  14. markelog commented on Dec 26, 2014

    @markelog
    Member

    IMO we should keep it. Our detection code is not perfect in weird setups so it's nice if people can workaround potential problems just by setting jQuery.support.cors to true (even if we don't officially support it).

    Then we should document and support it, otherwise we will face more problems in the future. But i wouldn't do that, the only reason i see to leave it in, is for the tests.

    Besides, there are way to do that, documented way. Whereas setting jQuery.support.cors is like using ajaxSetup which is not a preferable way, if we say that it's okay, then we're inviting user to change other support properties if he feels like it :-(.

    @jaubourg it would be interesting to see in what unsupported envs we would fail, if we would, it would be on the low amount of users, but if we wound't, then we could avoid the "just in case" code.

  15. 8 remaining items

  16. modified the milestones: 3.3.0, 3.2.0 on Mar 6, 2017
  17. self-assigned this
    on Mar 27, 2017
  18. modified the milestones: 3.3.0, 3.4.0 on Dec 11, 2017
  19. modified the milestones: 3.4.0, 4.0.0 on Sep 24, 2018
  20. added a commit that references this issue on Aug 21, 2019
    50f0509
  21. mgol commented on Aug 21, 2019

    @mgol
    Member

    jQuery.support.ajax & jQuery.support.cors have been removed in #4347 and will not be defined in jQuery 4.0. The remaining part is the try-catch around jQuery.ajaxSettings.xhr which I'm removing in #4467.

  22. added a commit that references this issue on Aug 22, 2019
    a775682
  23. added a commit that references this issue on Aug 26, 2019
    abdc89a
  24. locked as resolved and limited conversation to collaborators on Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions