/** Demo js to implement the most basic of ajax functionality **/ /* Its good practice in AJAX to declare all the urls you will be using at the start of the script. */ var url="lookup.cgi?name="; /* To use AJAX you need at least one http object, which will do the communication */ var http = getHTTPObject(); function handleHttpResponse() { // 4 is the signal for finished if (http.readyState == 4) { var pic = document.getElementById( 'loading' ); pic.src = ""; // Split the comma delimited response into an array results = http.responseText.split(","); fnametext=results[0]; var fname=document.getElementById( 'fname' ); fname.innerHTML = fnametext; snametext=results[1]; var sname=document.getElementById( 'sname' ); sname.innerHTML = snametext; picsrc=results[2]; var pic = document.getElementById( 'img1' ); pic.src = results[2]; quotetext=results[3]; var quote=document.getElementById('quote' ); quote.innerHTML = quotetext; } } function performLookup() { /* Get the name, we know its >= characters */ var name = document.getElementById("name").value; //Begin communication with lookup.cgi, passing in // name as a parameter http.open("GET", url + escape(name), true); // This bit says "If its loading, display a loading gif if(http.readyState == 1) { var pic = document.getElementById( 'loading' ); pic.src = "searching.gif"; } // When the state changes call our other method // handleHttpResponse http.onreadystatechange = handleHttpResponse; // end comms, only do this at the end of your interaction http.send(null); } /* This method is pretty much standard AJAX, you'll see it everywhere. The curious can read into its details, the rest of you can just know that this gives you a HTTP object */ function getHTTPObject() { var xmlhttp; /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; }