javascript - Redirect only one time

107

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.

52

Answer

Solution:

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

People are also looking for solutions to the problem: javascript - Alternative to Jumploader

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.