想要在用户重新加载页面时重定向到我的主页

时间:2012-06-13 20:02:31

标签: javascript

当用户按下浏览器的刷新按钮时,我想重定向到我的主页。

实施例。 当某个用户在我的应用程序的第二页(如www.mydomain.com/second.php)并点击浏览器refrsh按钮时,我想要一个javascript pop进行确认并将用户重定向到我的应用程序主页(如www.mydomain) .com)我想用javascript做。

1 个答案:

答案 0 :(得分:0)

您可以使用隐藏的输入来完成此操作。当用户第一次登陆页面时,将值设置为true。隐藏的输入通常在浏览器刷新时保存,因此在刷新页面时,设置为检查隐藏的输入是否设置为true。如果是,请使用window.location将用户重定向到您想要的页面。

此页面介绍了您要查找的内容:http://www.tedpavlic.com/post_detect_refresh_with_javascript.php

以下是直接来自此页面的示例:

<html>
<head>

<script language="JavaScript"> <!--
function checkRefresh()
{
    if( document.refreshForm.visited.value == "" )
    {
        // This is a fresh page load
        document.refreshForm.visited.value = "1";

        // You may want to add code here special for
        // fresh page loads
    }
    else
    {
        // This is a page refresh

        // Insert code here representing what to do on
        // a refresh
    }
} -->
</script>
</head>
<body onLoad="JavaScript:checkRefresh();">
    <form name="refreshForm">
        <input type="hidden" name="visited" value="" />
    </form>
</body>
</html>
相关问题