简体   繁体   中英

How can I save a cookie

I'm trying to save a cookie and then load it again

I have this code

<html>
    <head>
        <script>
            var myCookies = {};

            function saveCookies()
            {
                myCookies["_uuser"] = document.getElementById("user").value;
                myCookies["_uuage"] = document.getElementById("age").value;
                //Start Reuseable Section
                document.cookie = "";
                var expiresAttrib = new Date(Date.now()+60*1000).toString();
                var cookieString = "";
                for (var key in myCookies)
                {
                    cookieString = key+"="+myCookies[key]+";"+expiresAttrib+";";
                    document.cookie = cookieString;
                }
                //End Reuseable Section
                document.getElementById("out").innerHTML = document.cookie;
            }

            function loadCookies()
            {
                //Start Reuseable Section
                myCookies = {};
                var kv = document.cookie.split(";");
                for (var id in kv)
                {
                    var cookie = kv[id].split("=");
                    myCookies[cookie[0].trim()] = cookie[1];
                }
                //End Reuseable Section
                document.getElementById("user").value = myCookies["_uuser"];
                document.getElementById("age").value = myCookies["_uuage"];
            }
        </script>
    </head>
    <body>
        User: <input type="text" id="user">
        Age: <input type="text" id="age">
        <button onclick="saveCookies()">Save To Cookies</button>
        <button onclick="loadCookies()">Load From Cookies</button>
        <p id="out"></p>
    </body>
</html>

when I type an input for both name and age, and click on save to cookies, and then clock on load from cookies, I got this "undefined" for both user and age!!

what's missing in my code, so I can save the cookie

For Chrome cookies can only be set, when the page is running on a webserver. For example accessed via http://localhost/foo/bar.html or http://127.0.0.1/foo/bar.html

edit : you might check out as well this answer: where cookie saved for local HTML file I just tested it myself: it works with Firefox.

Otherwise it would be better for testing such cases, to put up a local webserver like apache

I have tested your code from a web server and it works fine.

You must load it from a web server, rather from the local file system.

JSFiddle is here if you want to prove it for yourself.

https://jsfiddle.net/brx5ropp/

Note that due to JSFiddle limitations I had to move the click triggers for the buttons to code like this:

document.getElementById("load").addEventListener("click", loadCookies);
document.getElementById("save").addEventListener("click", saveCookies);

...but that is irrelevant to my answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM