View Full Version : Javascript Reload Page
Novus Mortuus
05-20-2008, 05:17 PM
I've been looking for a simple reload/refresh script for a few days, but even though it seems very simple, i can't seem to find one :burnt:
A simple script that when placed on the page, waits a defined number of seconds and then reloads/refreshes. I don't want to use <meta> refresh, because it only needs to happen when the user does certain things that update the database.
Could anyone show me/direct me to one please?
Thanks.
coothead
05-20-2008, 06:03 PM
Hi there Novus Mortuus,
here is the basic javascript page reload...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function() {
var time=10*1000; /*this value is equivalent to 10 seconds */
setTimeout('location.reload(true)',time);
}
</script>
</head>
<body>
<div>
</div>
</body>
</html>
Novus Mortuus
05-20-2008, 06:46 PM
Thank you :)
Could i trouble you for one more script while you're here?
It's the other one i've not been able to find..well i've found some but they were very over complicated.
A countdown script.
I simply need it to start at 30 minutes (30:00), count down to 0 (00:00) and then restart.
Edit: I should mention, it should continue even when you're not on the page, so not restart at 30:00 every time you refresh.
The ones i've found have been quite complicated, but never dealt with restarting, it's always been to some set end, like a count down to CHristmas or something.
Thank you, much obliged. :)
coothead
05-20-2008, 08:04 PM
Hi there Novus Mortuus,
The ones I've found have been quite complicated, but never dealt with restarting..
Well, the restarting request is rather unusual but interesting enough to do it before I retire for the night. :agree:
The complications are caused by the restarting request being dependent upon the use of a cookie.
This cookie keeps track of the countdown time that has elapsed at the point of window closure. :agree:
Other than that you should find the code relatively simple. ;)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#time {
width:64px;
line-height:30px;
font-family:courier,monospace;
font-size:18px;
text-align:center;
border:3px double #999;
margin:auto;
}
</style>
<script type="text/javascript">
var m=30; /*this value - (set for 30 minuted) - may be edited to suit your requirements*/
var s=0; /*this value - (set for 0 seconds) - may be edited to suit your requirements*/
var temp=m;
var info;
window.onload=function() {
readCookie();
}
function readCookie() {
if(document.cookie) {
info=document.cookie.split('numbers=')[1];
m=parseFloat(info.split(',')[0]);
s=parseFloat(info.split(',')[1]);
info=m+','+s;
setCookie();
countdown();
}
else{
info=m+','+s;
setCookie();
countdown();
}
}
function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(30*24*60*60*1000); /* this cookie is set for 30 days */
exp.setTime(plusMonth);
document.cookie='numbers='+info+';expires='+exp.toGMTString();
}
function countdown(){
s--;
if(s<0) {
s=59;
m--;
}
if(m==-1) {
m=temp;
}
if(m<10) {
mins='0'+m;
}
else {
mins=m;
}
if(s<10) {
secs='0'+s;
}
else {
secs=s;
}
document.getElementById('time').firstChild.nodeValue=mins+':'+secs;
info=m+','+s;
setCookie();
cd=setTimeout('countdown()',1000);
}
</script>
</head>
<body>
<div id="time"> </div>
</body>
</html>
Novus Mortuus
05-20-2008, 08:44 PM
Thanks again. :)
coothead
05-21-2008, 02:27 AM
No problem, you're welcome. :agree:
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.