Reputation: 1
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
Reputation: 225238
Your syntax is wrong. I would write it like this:
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject();
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