Ispired,
You are on the right lines there but the code was getting a bit confused. You really need to know javascript well before diving into ajax. It's not really the place to start.
Anyways, here is some revised code (tested under IE6 - should work on most modern browsers).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function getXMLHttp() {
var xmlHttp;
try { xmlHttp = new XMLHttpRequest(); }
catch(e) {
try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {
try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}
function refreshIt(divID, MyPage) {
xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4) { setInnerText(divID, xmlHttp.responseText); }
}
xmlHttp.open("GET", MyPage, true);
xmlHttp.send(null);
return false;
}
function setInnerText(divID, response){
var el = (document.getElementById) ? document.getElementById(divID) : document.all[divID];
if(el) { el.innerText = response; }
}
</script>
</head>
<body>
<a href="" onClick="return refreshIt('id1', 'page1.php')">Refresh</a>
<div id="id1">Default Page Text - Div One</div>
<a href="" onClick="return refreshIt('id2', 'page2.php')">Refresh</a>
<div id="id2">Default Page Text - Div Two</div>
</body>
</html>
Airshow
Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372