PDA

View Full Version : ajax



Colin-Roberts
02-06-2007, 10:30 PM
anyone know how to make a page (txt page) reload every so many seconds without refreshing the page.. mentor made one awhile ago but it doesn't work.

Luckyrare
02-06-2007, 10:33 PM
try using script.aculo.us

Mentor
02-06-2007, 10:51 PM
anyone know how to make a page (txt page) reload every so many seconds without refreshing the page.. mentor made one awhile ago but it doesn't work.

Im pretty sure the script did work by the end of it? although i finalized outside forums. Its pretty simple to do without even changing the function anyway:


Heres a quick usage of my ajax page loading (http://thybag.co.uk/?p=Tutorials&ind=44) script to acheave the desired effect



// sets time between reloads in miliseconds
setInterval(LoadPage("INFOFILE.php","DIVNAME"), 30000);

function LoadPage(page,usediv) {
// Set up request varible
try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");} catch (e) { alert("Error: Could not load page.");}
//send data
xmlhttp.onreadystatechange = function(){
//Check page is completed and there were no problems.
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
//Write data returned to page
document.getElementById(usediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", page);
xmlhttp.send(null);
}


Only change from orignal is use of interval function to call it every so often.

Just chnage INFOFILE.php to the file name of the page you want to load from, and DIVNAME to the ID of the div you want to place the content returned in.

*other none essental changes made.
Removed, return false as served no purpous. Scroll to top, becuse it would be annoying, and page loading message since it would be in the way alot

Easy as pie :)

Colin-Roberts
02-06-2007, 11:24 PM
thanx mentor. still isn't working for me?

Mentor
03-06-2007, 02:24 AM
in the example it refresh's every 30 seconds, so you may need to make it shorter interval?
The code itself though should work? What JS errors is it giveing you?

Colin-Roberts
03-06-2007, 10:56 AM
it isnt erroring out anything it just leaves my div blank and loads nothing

Blob
03-06-2007, 03:17 PM
it isnt erroring out anything it just leaves my div blank and loads nothing

To make a default page load on startup, put:

<body onload="LoadPage('page2.htm','page_content');">

Want to hide these adverts? Register an account for free!