I would love to redirect a html page "open.html" after 5 o'clock to "close.html" and in the morning at 9 back to "open.html". What is the best way to do this (or test) this.
Asked
Active
Viewed 334 times
1
-
For testing, make the time configurable and change the configuration to a more suitable time. – freedomn-m Dec 16 '16 at 14:18
-
31. Why would you want to do this? Websites aren't regular shops, you don't really have "opening hours" on a website. 2. Why do this via JavaScript at all - even _if_ you were to go through with it, then it should be a server-side redirect. 3. I still strongly suggest against doing something like this. – VLAZ Dec 16 '16 at 14:19
3 Answers
1
Check this out:
Call a javascript function at a specific time of day
at the setTimeout(function(){alert("It's 10am!")}, millisTill10); you could add a simple redirect.

Litsher - Nathan
- 265
- 1
- 5
- 18
1
You can use a similar code to detect the hour of the day, this script make the check only one time is loaded, if you need continuously checking your hours you can use it with a setTimeout()
;
// detect time
var d = new Date();
var h = d.getHours());
if (h > 9 && h < 17 ){
// open url
window.location = "open.html";
}else{
window.location = "close.html";
}

GibboK
- 71,848
- 143
- 435
- 658
1
A quick way to redirect according to client time :
hours = new Date().getHours();
if (hours < 9 || hours >= 17) {
window.location.href = "close.html";
} else {
window.location.href = "open.html";
}
But beware that you may need server time in your case.

b2vincent
- 610
- 7
- 14