javascript - 在特定时刻重定向页面

标签 javascript php jquery html

如何在特定时间重定向到某个页面?

假设希望重定向到 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/

相关文章:

javascript - 在 Traccar 应用程序中更改或添加元素

php - 访问子域中的主域文件

javascript - 从远程 html 页面加载 div 并将其附加到当前页面

javascript - jQuery Select 插件将允许文件夹/文件结构化分层选项?

jquery - Kendo UI [DropDownList] - 过滤搜索,如果未找到搜索项则显示消息

javascript - Firebase 管理员,push().getKey() 能否保证我有一个唯一的 key ?

javascript - #3502 对象在 URL

javascript - 隐式流和身份服务器 3 : Is it possible to grant a JavaScript client an access token without having the user log in?

php - Wordpress - 如何将绝对链接更改为相对链接

php - 在已经包含其他页面的页面中包含 .js 和 .css 的最有效方法是什么?