页面仅从特定页面重定向

时间:2014-05-12 13:47:02

标签: javascript redirect

我正在尝试从启动页面重定向到另一个页面。我有这个代码,但我希望它只能从用户到达的第一页(主页)激活,而不是从网站上的每个页面激活。

这是我正在使用的代码,但它会继续在每个页面上启动:

var url ='http://cargocollective.com/clairelefevre/about';
var delay = 6;
var d = delay * 1000;
window.setTimeout ('parent.location.replace(url)', d);

谢谢, 马坦

4 个答案:

答案 0 :(得分:1)

您可以测试parent.location值:

if(parent.location == 'http://cargocollective.com/')
{
   var url ='http://cargocollective.com/clairelefevre/about';
   var delay = 6;
   var d = delay * 1000;
   window.setTimeout ('parent.location.replace(url)', d);
}

但说实话,IMO并不是很干净。我要做的是在你的主页上添加一个隐藏的输入,带有id和value。通过测试此输入来测试用户是否在主页上。

您还可以查看location.pathname值。

if(parent.location.pathname == '/')
{
   //your code
}

答案 1 :(得分:0)

你可以查看引用者,所以:

if (document.referrer.indexOf(window.location.origin) == -1) {
    window.setTimeout ('parent.location.replace(url)', d);
}

答案 2 :(得分:0)

var currUrl = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
var homeUrl = "http://cargocollective.com/";
if (currUrl == homeUrl) {
    var url ='http://cargocollective.com/clairelefevre/about';
    var delay = 6;
    var d = delay * 1000;
    window.setTimeout ('parent.location.replace(url)', d);
}

答案 3 :(得分:0)

我想到了解决这个问题的两种方法。

  1. 只将代码放在脚本标记内的主页上:

    <script>

    var url ='http://cargocollective.com/clairelefevre/about';
    var delay = 6;
    window.setTimeout ('parent.location.replace(url)', delay * 1000);
    

    </script>

  2. 在重定向之前使用if语句测试页面的位置是否正确

    if (window.location.pathname === '/') {
        var url ='http://cargocollective.com/clairelefevre/about';
        var delay = 6;
        window.setTimeout ('parent.location.replace(url)', d * 1000);
    }
    
相关问题