Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I get a cookie into a variable and then assign a div the result? [duplicate]

I am trying to get the value of a cookie into a global variable in which I can then place into div's by using document.getElementById.

Here is my code - it works prefectly in terms of the fact it remembers the cookie but it just won't do one line in particular which I have clearly outlined with a comment for you.

Here is all my code...

<!DOCTYPE HTML>
<html>
<head>
    <script>
        var globalVar = "";

        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toGMTString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
        }

        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("Welcome again " + user);
                globalVar = getCookie("username");
                document.getElementById("demo").innerHTML = globalVar; /* THIS LINE DON'T WORK WHY NOT! THE ONE BELOW DOES IN THE CHANGE COOKIE FUNCTION DOES! */
            } else {
                user = prompt("Please enter your name:", "");
                if (user != "" && user != null) {
                    setCookie("username", user, 30);
                }
            }
        }

        function changeCookie() {
            var user = getCookie("username");
            user = prompt("Please enter your name:", "");
            if (user != "" && user != null) {
                setCookie("username", user, 30);
            }
            document.getElementById("demo").innerHTML = user; /* THIS WORKS! */
        }
        checkCookie();
    </script>
</head>
<body>
    <div id="demo"></div>
    <button onclick="changeCookie()">Change username</button>
</body>
</html>
like image 896
Please Help Avatar asked Jan 30 '26 12:01

Please Help


1 Answers

Try calling checkCookie() right after the page has finished loading.

<body onload="checkCookie()">

Another way is to move the script tag to the end of the page, right before the closing tag of body like this:

<html>
<body>
   <div id="demo"></div>
   <button></button>

  <!-- the rest of the page's HTML here -->

   <script>
      ....
      .... your script code here
      ....
      ....
      checkCookie();
   </scirpt>
</body>
</html>
like image 110
Ahmad Avatar answered Feb 01 '26 03:02

Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!