Cannot set cookies in Javascript

后端 未结 9 2123
青春惊慌失措
青春惊慌失措 2020-12-01 10:28

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 coo

相关标签:
9条回答
  • 2020-12-01 10:43

    It works fine for me but Once again Make sure if your JS and Cookies are enabled in browser. Your should check whether you cookie is setting properly or not using if(document.cookie), it will then be easier for you debugging where the problem is. Maybe you're cookies are not written properly. Please do consider the following code.

    Write the Cookie

    Use the following code to write your cookie:

    <script language="JavaScript">
     cookie_name = "Basic_Cookie";
     function write_cookie() {
     if(document.cookie) {
     index = document.cookie.indexOf(cookie_name);
     } else {
     index = -1;
     }
    
     if (index == -1) {
     document.cookie=cookie_name+"=1; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
     } else {
     countbegin = (document.cookie.indexOf("=", index) + 1);
     countend = document.cookie.indexOf(";", index);
     if (countend == -1) {
     countend = document.cookie.length;
     }
     count = eval(document.cookie.substring(countbegin, countend)) + 1;
     document.cookie=cookie_name+"="+count+"; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
     }
     }
     </script>
    

    Read Your Cookie

    Once you've written the cookie, you need to read it in order to use it. Use this script to read your cookie:

    <script language="JavaScript">
     function gettimes() {
     if(document.cookie) {
     index = document.cookie.indexOf(cookie_name);
     if (index != -1) {
     countbegin = (document.cookie.indexOf("=", index) + 1);
     countend = document.cookie.indexOf(";", index);
     if (countend == -1) {
     countend = document.cookie.length;
     }
     count = document.cookie.substring(countbegin, countend);
     if (count == 1) {
     return (count+" time");
     } else {
     return (count+" times");
     }
     }
     }
     return ("0 times");
     }
     </script>
    

    Call Your Cookie in a Link

    Set your cookie when someone clicks a link with this code in your HTML body:

    <script language="javascript">document.write(gettimes());</script>
    

    Reference: Simple Cookie Read & Write

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 10:48

    Use HTML5 Local Storage instead:-

    <HTML>
        <HEAD>
            <TITLE>Hello World</TITLE>
        </HEAD>
        <BODY>
            <SCRIPT type="text/javascript">
                localStorage.setItem("myCookie", "ding=dong");
            </SCRIPT>
            <script type="text/javascript">
                alert(localStorage.getItem("myCookie"));
            </script>
        </BODY>
    </HTML>
    
    0 讨论(0)
  • 2020-12-01 10:54

    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.

    0 讨论(0)
  • 2020-12-01 10:58

    Chrome doesn’t store cookies from the pages which are loaded from local file system. ex: file:///C:/User

    0 讨论(0)
  • 2020-12-01 11:02

    Just change the URL from the loopback network interface to the address of the NIC of your LAN will do the work, e.g.

    http://localhost:8080 -> http://192.168.1.15:8080 
    
    0 讨论(0)
  • I tried to save an cookie on Chrome and had the same error, that it would save. Although it did work in Firefox, so I asked an collegue and he suggested that I take away path and domain (I had name of cookie, value, expire, path amd domain) and all of a sudden it worked. So just to get the cookie to actually save in Chrome, you just need name, value and expire.

    Hope it helps.

    Example:

    function createCookie(name,value,days) {
         if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
         }
         else var expires = "";
         document.cookie = name+"="+value+expires+";";
    }
    
    0 讨论(0)
提交回复
热议问题