Jump to content

Ajax: "jquery style" vs "Microsoft XMLHTTP style"


StevenOliver

Recommended Posts

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 by StevenOliver
Link to comment
Share on other sites

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 by StevenOliver
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.