Most efficient way to load another page using XMLHttpRequest?

Hi guys,

I’ve currently got this code that I use to load another page:

function loadurl(dest) {
	try {
		// Moz supports XMLHttpRequest. IE uses ActiveX. browser detction is bad. object detection works for any browser
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		// browser doesn't support ajax.
	}
	// the xmlhttp object triggers an event everytime the status changes - triggered() function handles the events
	xmlhttp.onreadystatechange = triggered;
	// open takes in the HTTP method and url.
	xmlhttp.open("GET", dest);
	// send the request. if this is a POST request we would have sent post variables: send("name=aleem&gender=male)
	xmlhttp.send(null);
}
function triggered() {
	// if the readyState code is 4 (Completed)  and http status is 200 (OK) we go ahead and get the responseText
	// other readyState codes: 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		// xmlhttp.responseText object contains the response.
		document.getElementById("output").innerHTML = xmlhttp.responseText;
	}
}

(taken from this tutorial)

It works great, but requires an extra div to be placed on the page with the id of output. It’s not a huge problem at all, but I was just wondering if there was another way of doing this since all the script needs to do is add something to the database, not output anything.

Cheers

If there is no output then you can get rid of the whole triggered part.


function loadurl(dest) {
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX. browser detction is bad. object detection works for any browser
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        // browser doesn't support ajax.
    }
    // open takes in the HTTP method and url.
    xmlhttp.open("GET", dest);
    // send the request. if this is a POST request we would have sent post variables: send("name=aleem&gender=male)
    xmlhttp.send(null);
}

That worked an absolute treat, thank you so much once again pmw57 - really appreciate it :slight_smile: