Captain Nemo
Captain Nemo

Reputation: 262

Cookies with JavaScript

I'm trying to set cookies in an easy code example, but they don't work as I expect. The problem is that the code doesn't work, it never says "Welcome again Nemo!" Can you help me please? What i am doing wrong? Thank you very much for your time!!!

  <script>

        function getCookie() {

            var name = document.cookie.split('=')
            if (name[1] != "" && name[1] != null){
                return name[1];
            }else{
                return "";
            }
        }

        function checkCookie() {
            var username=getCookie();
            if (username!="") {
                alert("Welcome again " + username);
            }
            else{
                username = prompt("Please enter your name:");
                if (username != "" && username != null) {
                    document.cookie =  "Username=" + username;
                }
            }
        }

        checkCookie();

    </script>

Upvotes: 0

Views: 52

Answers (1)

edepe
edepe

Reputation: 381

Are you trying to use cookies in a local file using chrome? It's not allowed, see: How to read/write cookies for local file:/// HTML document? and Why does Chrome ignore local jQuery cookies?

Upvotes: 2

Related Questions