Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onreadystatechange location in script

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi there, I'm trying to do an asynchrynous request to a php page and then update a div with the contents. I've done this sucessfully in the past but this script is driving me insane! Where should I place the onreadystatechange method in this script please:

Code:
var http = getHTTPObject();
var url = 'js/clevertext.php';

function updatetext($field, $client_id) {
	http.open("GET", url, true);
	http.send(null);
	http.onreadystatechange = handleStateChange();

}

function handleStateChange() {
	if (http.readyState == 4) {
		alert(http.responseText);
	}
}

function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
		xmlhttp = false;
		}
	}
	return xmlhttp;
}

I've tried it before and after the send but it either doesn't work, or only works the second time the function is called.

Thanks

Gary

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
Found the problem finally. The onreadystatechange goes between the open and the send, but the function name should not have the () at the end:

Code:
http.open("GET", url, true);
http.onreadystatechange = handleStateChange;
http.send(null);

Works fine!

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top