Reputation: 127
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
Upvotes: 0
Views: 160
Reputation: 1131
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
}
Upvotes: 1
Reputation: 25569
Two ways of solving this problem come to mind.
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>
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);
}
Upvotes: 0
Reputation: 3900
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);
}
Upvotes: 0
Reputation: 152206
You can check the referer, so:
if (document.referrer.indexOf(window.location.origin) == -1) {
window.setTimeout ('parent.location.replace(url)', d);
}
Upvotes: 0