Search code examples
javascriptcookiessetcookie

Set cookie and redirect on multiple visits not working as intended


I have created a script which to me looks like should work but unfortunately there's an issue where it set's cookie2 even the criteria has been met.

<script type="text/javascript">
    jQuery(document).ready(function () {
        if (typeof Cookies.get('cookie1') !== 'undefined') {
            Cookies.set('cookie2', 'true', {expires: 1000});
            window.location.href = "https://google.com/second";
        }
        else
        {
            Cookies.set('cookie1', 'true', {expires: 1000});
            }
            window.location.href = "https://google.com/first";
        }
    });
</script>

    <script type="text/javascript">
    jQuery(document).ready(function () {
        if (typeof Cookies.get('cookie2') !== 'undefined') {

            window.location.href = "https://google.com/third";
        }
        else
        {
        }
    });
</script>

On user's first visit should go to google.com/first, on user's second visit should go to google.com/second and last visit and every visit after that should go to google.com/third. The second visit isn't being met as look's like cookie 2 has already been inserted even thought it's not in the first "else" function


Solution

  • Hi the problem is with the conditional stating of your if/else condition. It will always go to else condition since the value is set for cookie1 after the first page load

    window.location.href = "https://google.com/first";

    so you need to add nested if/else condition

    Fiddle link :https://jsfiddle.net/kju86Ly9/3/

    jQuery(document).ready(function () {
        console.log(getCookie('cookie1'))
        console.log(getCookie('cookie2'))
    
    	if (getCookie('cookie1') !== 'undefined' && getCookie('cookie2') === null) {
            setCookie('cookie2', 'true', 10);
            console.log('https://google.com/first')
    		window.location.href = "https://google.com/first";
    	} else if (getCookie('cookie2') !== 'undefined' && getCookie('cookie3') === null) {
            setCookie('cookie3', 'true', 10);
            console.log('https://google.com/second')
    
    		window.location.href = "https://google.com/second";
    	} else {
            console.log('https://google.com/third')
    
    		window.location.href = "https://google.com/third";
    	}
    
    
    	function setCookie(name, value, days) {
    		var expires = "";
    		if (days) {
    			var date = new Date();
    			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    			expires = "; expires=" + date.toUTCString();
    		}
    		document.cookie = name + "=" + (value || "") + expires + "; path=/";
    	}
    
    	function getCookie(name) {
    		var nameEQ = name + "=";
    		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, c.length);
    			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    		}
    		return null;
        }
        function eraseCookie(name) {   
            document.cookie = name+'=; Max-Age=-99999999;';  
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>