我试图让HTML页面在一天的特定时间后自动重定向到同一域中的另一个URL。
示例:
<script type="text/javascript">
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
if((new Date().today()) >= "todaysdate 12:26PM")
{
location.href ="http://www.google.com";
}
else
{
location.href="index.html";
}
if((new Date().today()) >= "todaysdate 4:26PM")
{
location.href ="http://www.Hotmail.com";
}
else
{
location.href="index.html";
}
if((new Date().today()) >= "todaysdate 8:16AM")
{
location.href ="http://www.youtube.com";
}
else
{
location.href="index.html";
}
</script>
&#13;
答案 0 :(得分:0)
您可以在新日期使用setHours并对其进行比较:
var time = Date.now();
if (time < new Date().setHours(12, 0, 0, 0)) { // Before 12
location.href = "http://www.google.com";
} else if (time < new Date().setHours(16, 0, 0, 0)) { // 12 to 4pm
location.href = "http://www.hotmail.com";
} else if (time < new Date().setHours(20, 0, 0, 0)) { // 4pm to 8pm
location.href = "http://www.youtube.com";
} else {
location.href = "index.html"; // 8 pmon wards
}