0

I do not understand the following situation.

On the backend I am using Tomcat. Tomcat automatically sends a cookie with name JSESSIONIDwhich looks something like that cookie-value = FE3C82283FA06B9BE79EF1CE50AEB32E.

The max-age property is set to session and this cookie is always sent by the server.

Now, on my website I run the following javascript code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>

    <body>
        <h1>Hello From Cookie-LocalStorage-Demo</h1>
        <div id="list_cookies_div">

        </div>
        <script>
            function print_cookies() {
                console.log("document.cookie = " + document.cookie);
                var cookies = document.cookie.split(';');
                console.log("print_cookies: cookies.length = " + cookies.length);
                for (var i = 0 ; i < cookies.length; i++) {
                    var div = document.getElementById("list_cookies_div");
                    div.innerHTML += "Cookie Value = " + cookies[i] + "<br/>"
                    console.log(i + ":" + cookies[i]);
                }
            }

            window.onload = print_cookies;
        </script>   
    </body>
</html>

When I run this website the output is "" so bascially nothing. I acutally looks like this here: enter image description here

On the other hand when I have a look at the Developer Tools of the browser I can see the cookie (see screenshot):

enter image description here

Why is the cookie shown in the developer tools but not accessible from javascript on the page?

toom
  • 12,864
  • 27
  • 89
  • 128
  • 2
    Your last screenshot shows that the cookie is "[HttpOnly](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies)" – Ivar Aug 25 '19 at 12:48
  • 1
    That cookie has a path (`/Cookie-LocalStora...`). You won't be able to read it from elsewhere, e.g. `/`. – Álvaro González Aug 25 '19 at 12:49
  • I guess the `HttpOnly` flag is the problem. Thanks for this answer – toom Aug 25 '19 at 12:51
  • 1
    @Álvaro González: I am accessing the page from this path. But thanks for mentioning anyway. – toom Aug 25 '19 at 12:52

0 Answers0