Click to See Complete Forum and Search --> : Redirecting a page to another at a given time (not after some seconds)


rosanaarbelo
10-13-2004, 06:02 AM
Hi everyone, I've been searching the Internet for days trying to find a javascript that redirects from a page to another at a given time, but the only thing I come across is the typical one which does it after certain seconds.

Does anyone know a way to do the redirect let's say at 15:32 hours, no matter when the page has been loaded? I'd be really grateful if you could help me!

Regards, Rosana

David Harrison
10-13-2004, 06:43 AM
Put this somewhere in your page:<script type="text/javascript"><!--

var endTime="15:32:00";
var redirPage="http://www.w3.org/";

window.onload=check;

var d, h, m, s;

function check(){

d=new Date();
h=d.getHours().toString();
m=d.getMinutes().toString();
s=d.getSeconds().toString();

if(h.length==1){h="0"+h;}
if(m.length==1){m="0"+m;}
if(s.length==1){s="0"+s;}

if(h+":"+m+":"+s == endTime){
location.href=redirPage;
}else{
setTimeout("check()",1000);
}

}

//--></script>However, do not rely on the clients computer clock to be correct, many aren't. Also you may have users in different time zones and users with JavaScript disabled (roughly 10% of users do not have JavaScript enabled).

Using JavaScript for this effect will produce vary unreliable results.

The best way to acheive this effect would be to use a server side language and let users view the first page if it is before the cut-off time, but if they arrive after the cut-off time, use a header redirect to take them to a different page.

rosanaarbelo
10-13-2004, 02:36 PM
Thanks a lot, lavalamp! That script is OK for the purpose I'm looking for.

David Harrison
10-13-2004, 04:49 PM
OK then, happy to help. :)