如何在特定时间重定向到某个页面?
假设希望重定向到 31st dec 12:00AM
以及 12:00
之后访问的人必须直接重定向到新站点。
这在 jquery
中可能吗?或PHP
?
最佳答案
PHP 代码将在页面加载之前进行检查,因此新访问者将被重定向,而 javascript 代码将在页面加载后每 10 秒检查一次,因此现有访问者将被重定向。
<?php
$date = new DateTime("December 31st, 2014 12:00AM");
$now = time();
if($now > $date->getTimestamp()) // if it's past the date
{
header("Location: http://google.com/");
}
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function checkDate()
{
// Year: 2014
// Month: 11 is December (0-11)
// Day: 31st
var date = new Date(2014, 11, 31, 0, 0, 0, 0);
var now = new Date();
if(now > date) // if it's past the date
{
window.location.replace("http://google.com/");
}
}
$(function() {
window.setInterval(checkDate, 10 * 1000); // check every ten seconds
});
</script>
</head>
<body>
</body>
</html>
关于javascript - 在特定时刻重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23915228/