Clarification of a some code

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2010-05-20T18:20:35Z Indexed on 2010/05/20 18:40 UTC
Read the original article Hit count: 141

Filed under:
|

I have come across a website that appears to use Ajax but does not include any js file except one file called ajax.js which has the following:

function run(c, f, b, a, d) {
    var e = null;
    if (b && f) {
        document.getElementById(b).innerHTML = f
    }
    if (window.XMLHttpRequest) {
        e = new XMLHttpRequest()
    } else {
        if (window.ActiveXObject) {
            e = new ActiveXObject(Microsoft.XMLHTTP)
        }
    }
    e.onreadystatechange = function () {
        if (e.readyState == 4) {
            if (e.status == 200 || e.statusText == "OK") {
                if (b) {
                    document.getElementById(b).innerHTML = e.responseText
                }
                if (a) {
                    setTimeout(a, 0)
                }
            } else {
                console.log("AJAX Error:  " + e.status + " | " + e.statusText);
                if (b && d != 1) {
                    document.getElementById(b).innerHTML = "AJAX Error.  Please try refreshing."
                }
            }
        }
    };
    e.open("GET", c, true);
    e.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    e.send(null)
}

Like you might have guessed, the way it issues queries inside the page with queries like this:

run('page.php',loadingText,'ajax-test', 'LoadSamples()');

I must admit that this is the first time I've seen a page from which I could not figure how things are being done. I have a few questions:

  • Is this Server-Side Ajax or something similar? If not, can someone clarify what exactly is this?
  • Why does one use this? Is it for hiding the design details? (which are otherwise revealed in plain text by javascript)
  • How difficult would it be to convert my existing application into this design pattern? (maybe a subjective question but any short suggestion will do)

Any suggestions?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about beginner