Search code examples
javascriptgoogle-chromecookies

Cannot set cookies in Javascript


I have a very simple line of code that set and read a cookie. I kept getting empty value for my cookie and have no understanding why. I have cookie enabled and know that cookies work on the browser.

<HTML>
    <HEAD>
        <TITLE>Hello World</TITLE>
    </HEAD>
    <BODY>
        <SCRIPT type="text/javascript">
            document.cookie = "ding=dong";
        </SCRIPT>
        <script type="text/javascript">
            alert(document.cookie);
        </script>
    </BODY>
</HTML>

Solution

  • Recently I stumbled upon a similar issue. My script couldn't store a cookie in Chromium, although it worked well on all other major browsers. After some googling it turned out that Chrome ignores cookies from local pages. After I uploaded the page to remote server code magically started to work.