Spacetech
04-16-2006, 05:55 PM
Hi Im new to ajax and I am having a problem with this script
ajax.js:
function createRequestObject() {
var connect;
var browser = navigator.appName;
if(browser == 'Microsoft Internet Explorer'){
connect = new ActiveXObject('Microsoft.XMLHTTP');
}
else{
connect = new XMLHttpRequest();
}
return connect;
}
var http = createRequestObject();
function ajax(url, id) {
load.style.display = 'block';
http.open("GET", url, true);
http.onreadystatechange = handleResponse(id);
http.send(null);
}
function handleResponse(id) {
if(http.readyState == 4 && http.status == 200){
document.getElementById(id).innerHTML = http.responseText;
load.style.display = 'none';
}
else{
alert("AJAX Error. Please Reload The Page");
}
}
test.html:
<script language="JavaScript" src="ajax.js" type="text/javascript"></script>
<style type="text/css">
#load {
position:absolute;
left:0;
top:0;
width:70px;
height:20px;
background-color: #CC4444;
color: white;
display: none;
}
</style>
<div id="load">Loading...</div>
<a href="javascript:ajax('test.txt', 'content')">Load Something</a>
<br />
<br />
<div id="content">
</div>
I get that alert message at the bottom when I try to run it.
Any ideas on how to fix it?
ajax.js:
function createRequestObject() {
var connect;
var browser = navigator.appName;
if(browser == 'Microsoft Internet Explorer'){
connect = new ActiveXObject('Microsoft.XMLHTTP');
}
else{
connect = new XMLHttpRequest();
}
return connect;
}
var http = createRequestObject();
function ajax(url, id) {
load.style.display = 'block';
http.open("GET", url, true);
http.onreadystatechange = handleResponse(id);
http.send(null);
}
function handleResponse(id) {
if(http.readyState == 4 && http.status == 200){
document.getElementById(id).innerHTML = http.responseText;
load.style.display = 'none';
}
else{
alert("AJAX Error. Please Reload The Page");
}
}
test.html:
<script language="JavaScript" src="ajax.js" type="text/javascript"></script>
<style type="text/css">
#load {
position:absolute;
left:0;
top:0;
width:70px;
height:20px;
background-color: #CC4444;
color: white;
display: none;
}
</style>
<div id="load">Loading...</div>
<a href="javascript:ajax('test.txt', 'content')">Load Something</a>
<br />
<br />
<div id="content">
</div>
I get that alert message at the bottom when I try to run it.
Any ideas on how to fix it?