原生JS如何向cookie里面保存小量数据

<span id="scroe"></span>
如何把scroe的innerText作为需要保存的数据保存进入cookie并随时读取呢

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
</style>
<script type="text/javascript">
    function readCookie (name)
    {
        var cookieValue = "";
        var search = name + "=";
        if (document.cookie.length > 0)
        {
            offset = document.cookie.indexOf (search);
            if (offset != -1)
            {
                offset += search.length;
                end = document.cookie.indexOf (";", offset);
                if (end == -1)
                    end = document.cookie.length;
                cookieValue = unescape (document.cookie.substring (offset, end))
            }
        }
        return cookieValue;
    }
    function writeCookie (name, value, hours)
    {
        var expire = "";
        if (hours != null)
        {
            expire = new Date ((new Date ()).getTime () + hours * 3600000);
            expire = "; expires=" + expire.toGMTString ();
        }
        document.cookie = name + "=" + escape (value) + expire;
    }
     
    writeCookie ("myCookie", "my name", 24);
    alert (readCookie ("myCookie"));
</script>
</head>
<body>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考
相似回答