Forum Moderators: open

Message Too Old, No Replies

Using nullish with XMLHttpRequest()

         

csdude55

4:30 am on Sep 14, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My original code has this:

try { xmlHttp=new XMLHttpRequest(); }
catch (e) {
try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { return false; }
}
}


I'm not sure what browsers the last two fall back on, though. I'm guessing old IE based on the "MS" and "Microsoft", though.

Can anyone confirm that this is the same thing?

var xmlHttp = new XMLHttpRequest() ?
new ActiveXObject("Msxml2.XMLHTTP") ?
new ActiveXObject("Microsoft.XMLHTTP") ?
false;

Peter_S

8:20 am on Sep 14, 2023 (gmt 0)

5+ Year Member Top Contributors Of The Month



Can anyone confirm that this is the same thing?

I don't think so, because the first code intercepts exceptions (errors), the second piece of code risks to cause unrecoverable exception, which would halt the script.

This is not your question ,but, instead of using XMLHttpRequest, you should use the Fetch API :
[developer.mozilla.org...]
[w3schools.com...]

Since 2015, this is the modern way to make HTTP requests from Javascript. (IE doesn't support it) : [caniuse.com...]

csdude55

5:41 pm on Sep 14, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unfortunately for me, I still have a significant amount of traffic using IE11. And some using IE9! A lot of my users are elderly and simply never update anything unless someone does it for them. So I'm always stuck coding like it's 10 years ago! LOL

the second piece of code risks to cause unrecoverable exception, which would halt the script.

Do you mean that the script would die if new XMLHttpRequest() failed, without trying the other two?

I might be overthinking this one, anyway, though. It looks like XMLHttpRequest() in my script would be fine with IE 10+ (as long as I don't use JSON as responseType, which I don't), so the two ActiveXObject() lines are JUST for those IE9 users.

[caniuse.com...]

Since 2015, this is the modern way to make HTTP requests from Javascript.

I'm not finding the answer to this, but what's the advantage to Fetch over XMLHttpRequest? Async vs sync?

csdude55

7:16 pm on Sep 14, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Minor update, I found that Microsoft says that they've supported XMLHttpRequest since IE7!

[learn.microsoft.com...]

I don't know if that means that caniuse.com is wrong? But if so then it looks like I can remove the ActiveXObject lines altogether.

My actual code:

function foo(url) {
if (url) {
try { xmlHttp=new XMLHttpRequest(); }
catch (e) {
try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { return false; }
}
}

if (xmlHttp) {
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
}

if (!url || !xmlHttp) return false;
}