Basic Ajax Function

i have a dream that one day all browsers support XMLHttpRequest object :)

world will be a better place for web developers or programmers without IE.

function happyworldajaxer(txt) {
            var xmlHttp = new XMLHttpRequest();          
 
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4) {
                    if (xmlHttp.status == 200) {
                        document.getElementById('test').innerHTML 
                                                 = xmlHttp.responseText;
                    }
                } 
            }
 
            xmlHttp.open("GET", "a.ashx?b=" + txt, true);
            xmlHttp.send(null);
        }

this function makes ajax response in all browsers.

Microsoft.XMLHTTP, xmlHttp.readyState == “complete” is necessary for IE.

 
function ajaxer(txt) {
            var xmlHttp;
            try { xmlHttp = new XMLHttpRequest(); }
            catch (e) {
                try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                catch (e) {
                    try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
                    catch (e) { return false; }
                }
            }
 
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                    if (xmlHttp.status == 200) {
                        document.getElementById('test').innerHTML 
                                                  = xmlHttp.responseText;
                    }
                } 
            }
 
            xmlHttp.open("GET", "a.ashx?b=" + txt, true);
            xmlHttp.send(null);
        }
Be Sociable, Share!

    Category: Javascript - Jquery - Comments Off on Basic Ajax Function

    Comments are closed.

    Back to top