zoukankan      html  css  js  c++  java
  • js设置 获取 删除cookie

    function setCookie(key,value) {
            var date = new Date(),
                    t = 5;
            date.setDate( date.getDate() + t );
            document.cookie = key+'='+encodeURIComponent(value)+';expires='+date.toGMTString();
        }
        // setCookie('username','liuwei');
        // setCookie('password','000000');
        // console.log(document.cookie);    //username=liuwei; password=000000
        // function getCookie(key) {
        //     var arr = document.cookie.split('; ');
        //     for (var i = 0; i < arr.length; i++) {
        //         var arr2 = arr[i].split('=');
        //         for (var j = 0; j < arr2.length; j++) {
        //             if (arr2[0] == key) {
        //                 return arr2[1];
        //             }
        //         };
        //     };
        // }
        function getCookie(key) {
            var arr,reg = RegExp('(^| )'+key+'=([^;]+)(;|$)');
            if (arr = document.cookie.match(reg))    //["username=liuwei;", "", "liuwei", ";"]
                return decodeURIComponent(arr[2]);
            else
                return null;
        }
    
        function delCookie(key) {
            var date = new Date();
            date.setTime(date.getTime() - 1);
            var delValue = getCookie(key);
            if (!!delValue) {
                document.cookie = key+'='+delValue+';expires='+date.toGMTString();
            }
        }
        delCookie('username');
    
        // console.log(document.cookie);
  • 相关阅读:
    错误提示窗口-“操作系统当前的配置不能运行此应用程序”
    打印机无法打印的10种解决方法
    开发进度三
    人月神话阅读笔记二
    开发进度二
    开发进度1
    人月神话阅读笔记一
    库存物资管理系统
    四则运算
    动手动脑5
  • 原文地址:https://www.cnblogs.com/lw9413/p/4569227.html
Copyright © 2011-2022 走看看