piyal hasan
piyal hasan

Reputation: 1

A different XMLHttpRequest

I am new to JS world. I'm trying to make this short, but it's not working. Is this possible? If not why?

var xhr;
if (window.XMLHttpRequest) ? xhr= new XMLHttpRequest(): xhr=new window.ActiveXObject();

Upvotes: 0

Views: 139

Answers (1)

Ry-
Ry-

Reputation: 225238

  1. Your syntax is wrong. I would write it like this:

    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject();

  2. You need to pass a string to ActiveXObject. So:

    new ActiveXObject('Msxml2.XMLHTTP');
    or
    new ActiveXObject('Microsoft.XMLHTTP');
    . You need them both for compatibility, by the way, so you won't be able to condense it into one line.

Upvotes: 1

Related Questions