Question

I am trying to redirect from a splash page to another. I have this code but I want it to be activated only from the first page the users arrive (the homepage) and not from every page on the site.

This is the code I am using but it keeps on launching on every page:

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

Thanks, Matan

Was it helpful?

Solution

You can test the parent.location value:

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);
}

But honestly, it's not very clean IMO. What I would do is put a hidden input in your homepage, with a id and value. Test if the user is on the homepage by testing this input.

You can also check the location.pathname value.

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

OTHER TIPS

You can check the referer, so:

if (document.referrer.indexOf(window.location.origin) == -1) {
    window.setTimeout ('parent.location.replace(url)', d);
}
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);
}

Two ways of solving this problem come to mind.

  1. Only place the code on the home page inside a script tag:

    <script>

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

    </script>

  2. Test the location of the page is correct with an if statement before redirecting

    if (window.location.pathname === '/') {
        var url ='http://cargocollective.com/clairelefevre/about';
        var delay = 6;
        window.setTimeout ('parent.location.replace(url)', d * 1000);
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top