StevenOliver Posted May 5, 2020 Share Posted May 5, 2020 (edited) Background: I'm comparing 2 styles of Ajax: 1.) "jquery style" 2.) "ActiveXObject Microsoft.XMLHTTP style" Question: Is one better (faster, more cross-browser compliant) than the other? My experience: Both seem equally fast. The Microsoft style is a bit longer, but I don't have to load jquery.js to my page! Code Examples: Jquery style on my PHP page: function getInfo(ProductNumber){ $.ajax({ url:'Ajax-PHP-Page.php?ProductNumber='+ProductNumber, success: function(html) { document.getElementById("my_div").value = ''; document.getElementById("my_div").value = html; } }); } Microsoft style on my PHP page: function getInfo(ProductNumber) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("my_div").value = this.responseText; } }; xmlhttp.open("GET","Ajax-PHP-Page.php?ProductNumber="+ProductNumber,true); xmlhttp.send(); } Thank you!! Edited May 5, 2020 by StevenOliver Quote Link to comment Share on other sites More sharing options...
requinix Posted May 5, 2020 Share Posted May 5, 2020 1. XMLHttpRequest is supported everywhere now. There is no reason for any modern application to even know about Microsoft.XMLHTTP. 2. jQuery does all this decision making for you. So what do you think? Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted May 5, 2020 Author Share Posted May 5, 2020 (edited) Requinix, thank you. Here are my thoughts: 1.) Regarding Microsoft, I think the line of code "else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }" simply helps cover all bases. 2.) Using jquery looks cool and a jquery function only needs a couple lines to type. 3.) However, adding jquery.js turns my 4K page into a 50K, and at least I "see" what's going on when using the XMLHTTP style. 4.) I like "decision making" and I dislike how jquery is a 50k of intimidating code, of which I need only a couple lines of anyway. So, I would choose the XMLHTTP style, and I'm ready to dig through all my code and replace the jquery version with the XMLHTTP version. However, if you said that jquery is a.) safer, or b.) more browser compliant, or c.) traps errors better, or d.) better in some other way, I would change my code back to jquery. So I'm curious which one you'd choose? Edited May 5, 2020 by StevenOliver Quote Link to comment Share on other sites More sharing options...
requinix Posted May 5, 2020 Share Posted May 5, 2020 Let me put it this way: // code for IE6, IE5 Do you care about IE6? Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 7, 2020 Share Posted May 7, 2020 Part of the reason jQuery became what it is is because of cross-browser compliance. You didn't have to do the `if(ie), else(everyone_else)` jig. However, as requinix pointed out, these days an AJAX call is pretty much an AJAX call regardless what browser you're using, assuming it's not an obsolete browser. Shoot, even fetch() is supported by everything except ie11, and I'm pretty sure there's a polyfill for that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.