<html>
<head>
<title>Test Ajax</title>
<script language="JavaScript" type="text/javascript">
// check browser
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert ("not support");
}
}
// Get browser Object
var receiveReq = new getXmlHttpRequestObject();
function sayHello() {
// .readyState = Attribute ( the type of unsigned short )
// 0 = Not work
// 1 = process
// 2 = compleate process
// 3 = server process
// 4 = server complete
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'test.html', true);
// .onreadystatechange = Attribute ( represent a fuction that must be invoked when readyState changes value )
receiveReq.onreadystatechange = handleSayHello;
receiveReq.send(null);
// send content
}
}
function handleSayHello() {
if (receiveReq.readyState == 4) {
document.getElementById('span_result').innerHTML = receiveReq.responseText;
// Receive data from server as text
}
}
</script>
</head>
<body>
<a href="javascript:sayHello();">Say Hello</a><br />
<span id="span_result"></span>
</body>
</html>
ไม่มีความเห็น