1

How can I use cookies to redirect from main page to intro page, then go back from intro page to main page ? And how to set cookies to allow people once ever 24h to see intro page instead of everytime loading a main page ?

1 Answers1

1

For redirecting to another URL you should use window.location, not cookies

window.location.replace("http://stackoverflow.com");
window.location.href = "http://stackoverflow.com";

See e.g. How to redirect to another webpage in JavaScript/jQuery?

To set cookie you can use e.g.

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

See http://www.w3schools.com/js/js_cookies.asp for more info

Community
  • 1
  • 1
Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
  • But the point is that i don't want to load this intro page everytime people load the main page, is it still possible to do with this ? – Chris Kucharzyk Dec 21 '15 at 09:15
  • You can use `document.referrer` to define the URL where the user come to your page from. – Alexander Elgin Dec 21 '15 at 09:17
  • I have shown you the tools. I hope you can realize how to use them to solve your problem. – Alexander Elgin Dec 21 '15 at 09:19
  • I'll try my best, thank you and Will give you a feedback of what I'll try to do – Chris Kucharzyk Dec 21 '15 at 09:20
  • Ok so I made this work with cookies. When I enter this intro page it will redirect me to my main page if cookies are set if not then you can see intro page. But I can't handle this window.location.replace . I put this in my html: But it doesn't replace my main url to the one I want :/ – Chris Kucharzyk Dec 21 '15 at 10:00
  • I don't know why you use `setTimeout` but you use it incorrectly. The correct usage is `setTimeout(changesite, 0)` – Alexander Elgin Dec 21 '15 at 10:05
  • Because I want it to redirect immediately after someone get on the website – Chris Kucharzyk Dec 21 '15 at 10:09
  • Is this not a good way to do this ? and in which file i should add window.location.replace ? in the same file where I put cookie script (means in intro.html) or into index_front.html (which is main website template file) ? – Chris Kucharzyk Dec 21 '15 at 10:15
  • You can simply use `changesite()` – Alexander Elgin Dec 21 '15 at 10:34
  • damn I still dont get it, When I put this function changesite into index_front.html it loops throu intro and main site. – Chris Kucharzyk Dec 21 '15 at 11:43