zoukankan      html  css  js  c++  java
  • JS操作Cookie

    <script type="text/javascript" language="javascript">
        //写入cookie
        function setCookie(name, value) {
            var days = 30;
            var exp = new Date();
            exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        }
        //读取cookie
        function getCookie(name) {
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (arr != null)
                return unescape(arr[2]);
            return null;
        }
        //删除cookie
        function delCookie(name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = getCookie(name);
            if (cval != null)
                document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        }
        //获取cookie解码后的值
        function getCookieValue(offset) {
            var endstr = document.cookie.indexOf(";", offect);
            if (endsrt == -1) {
                endstr = document.cookie.length;
            }
            return unsecape(document.cookie.substring(offset, endstr));
        }

        function test() {
            setCookie("testName", "testContent")
            alert(getCookie("testName"));
        }
    </script>

    <input type="button" value="test" onclick="test();" />

  • 相关阅读:
    页面静态化3 --- 伪静态技术
    9.14[XJOI] NOIP训练33
    9.13[XJOI] NOIP训练32
    Hello world!
    BZOJ-1853: [Scoi2010]幸运数字 (容斥原理)
    luogu1983[NOIP2013pjT4] 车站分级(拓扑排序)
    luogu1113 杂物 (拓扑排序)
    POJ-1094 Sorting It All Out && luogu1347 排序 (拓扑排序)
    BZOJ-1965: [Ahoi2005]SHUFFLE 洗牌 (快速幂+乘转加)
    BZOJ-2705: [SDOI2012]Longge的问题 (欧拉函数)
  • 原文地址:https://www.cnblogs.com/dachie/p/1791104.html
Copyright © 2011-2022 走看看