-1

So I'm now learning about cookies and and got my cookie code form this page on w3schools.com.

Here on StackOverflow I looked up about redirecting and I've attempted to redirect to here is the cookie has not been set. This will redirect to a page on my server to accept cookie policy and customize the admin panel theme.

<!-- Javascript BEFORE page loads Starts -->
    <script type="text/javascript">

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

        function checkCookie() {
            var user=getCookie("username");
            if (user != "") {
                alert("Cookie Set: " + user);
            } else {
               //user = prompt("Please enter your name:","");
               if (user != "" && user != null) {
                   window.location.replace("http://stackoverflow.com");
               }
            }
        }
    </script>
<!-- Javascript BEFORE page loads Starts -->

</head>
<!-- Header Ends -->

<!-- Body Starts -->
<body onload="checkCookie()">
Tim Marshall
  • 360
  • 5
  • 25

1 Answers1

1

Why are you checking for user!="" && user!= null in the else block? the user variable is guaranteed to be empty when code enters in this block. This is why window.location.replace isn't executing and the reason why its not redirecting.

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

   function checkCookie() {
   var user=getCookie("username");
    if (user != "") {
        alert("Cookie Set: " + user);
    } else {
       //user = prompt("Please enter your name:","");
           setCookie("username", "value", 1);
           window.location.replace("http://stackoverflow.com");
    }
}
camrydash
  • 93
  • 6
  • This does not work either, and as stated I am learning and this is with w3schools did, I cannot say why sorry. – Tim Marshall Jan 25 '15 at 03:59
  • Bingo and wow that is weird, when you nav back it's like you didn't go on the original page. Is this avoidable alike a link click? – Tim Marshall Jan 25 '15 at 04:04
  • @TimMarshall You have to implement the setCookie() method as shown here [link](http://www.w3schools.com/js/js_cookies.asp). When the user goes back to the site, the code should handle the case if the cookie is present. – camrydash Jan 25 '15 at 04:09
  • I know, I linked to that in my question. What I am now asking is that upon going to my page it takes me to here however navigating back it was like I never went to my page in the first place. [Click here to try](admin.rafflebananza.com/newadmin.html) – Tim Marshall Jan 25 '15 at 04:12
  • @TimMarshall You have to set the `username` cookie. Please see my updated code. When you navigate back, the if block will run and it won't redirect here. – camrydash Jan 25 '15 at 04:20
  • @TimMarshall window.location.replace replaces the current history item so you can't go back to it. use window.location.href instead – Marco Magrini Jan 25 '15 at 04:33
  • @MarcoMagrini I tried ` window.location.href = "http://stackoverflow.com";` which also seemed to replace. which you can test above in the comments – Tim Marshall Jan 25 '15 at 04:48