0

I have a site (landing) that when a user enter automatically goes to another site (its a simple redirect using window.location in the index.html).

The problem is when the user click in the back button on the browser, because when the user goes back, come back to the landing redirect, so the user returns be redirected.

How I can make this redirection only one time per user?

I do not know if it's important but the site (landing) is another domain that the domain of the redirection.

Thank you.

David Matas
  • 11
  • 1
  • 3
  • 2
    Save a cookie/something in local storage before redirecting? Then you only redirect if that thing doesn't exist. – Mike Cluck Jun 17 '16 at 21:29
  • I don't save a cookie for now. With this line of code in index.html it's fine? document.cookie = "username=John Doe"; , after this how I can redirect if that thing doesn't exist? what is the code? – David Matas Jun 17 '16 at 21:40
  • There are [all kinds of sources](http://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript) out there that will tell you how to read a cookie. – Mike Cluck Jun 17 '16 at 21:43
  • Your page has nothing but the `window.location` (no text, no images), so, if redirection works only once, when the back button is pressed, the user will come back to the first page and stay there watching the blank screen (because it will not redirect again). Is that what you really want? – Jose Manuel Abarca Rodríguez Jun 17 '16 at 22:02

1 Answers1

0

Using the cookie solution. Something like this if I understood your needs correctly:

    if (getCookie("first_visit") != "true") {
        document.cookie = "first_visit=true";
        location.href="NEW_URL";    
    }


   //from http://www.w3schools.com/js/js_cookies.asp
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i = 0; i <ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length,c.length);
            }
        }
        return "";
    }   
mrlew
  • 7,078
  • 3
  • 25
  • 28