Aug 27, 2007

AJAX basic

            var xmlHttp = null;
            if (window.XMLHttpRequest) { // IE7, Mozilla, Safari, Opera, etc.
            xmlHttp = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
            try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE 5.x, 6
            }
            catch(e) {}
            }

            function sendRequest(url) {
            if (xmlHttp) {
            xmlHttp.open("GET", url, true); // true = async
            xmlHttp.onreadystatechange = onCallback;
            xmlHttp.setRequestHeader('Content-type',
            'application/x-www-form-urlencoded');
            xmlHttp.send(null);
            }
            }

            function onCallback() {
            if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200){
            var r = document.getElementById('results');
            r.innerHTML = xmlHttp.responseText;
            }
            else {
            alert('Error: ' + xmlHttp.status);
            }
            }
            }