cookie

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        alert(document.cookie);
      /*document.cookie = "name=凌薇";
        alert(document.cookie);*/

        /*
        * 设置存储时长
        *   在设置每个值的时候,需要在最后加上"expires=过期时间" 数据
        * */

/*        var data = new Date(2019,0,26,6,0);
        document.cookie = "name=aa;expires="+data.toGMTString();
        alert(document.cookie);*/

    /*
    * 获取cookie的方式
    *   "name=lw; age=18"
    *
    * */
   // alert(document.cookie);
        // 创建cookie的时候,必须一个一个创建
      var date = new Date(2019,0,30,6,0);
      document.cookie = "name=洛天;expires="+date.toGMTString();
      document.cookie = "age=18;expires="+date.toGMTString();
      document.cookie = "sex=nan;expires="+date.toGMTString();

      //alert(getCookie("name"));

        getCookie("sex");
/*        function getCookie(key){
            var r = new RegExp("\\b" + key + "=([^;]*)(;|$)" );
            var val = document.cookie.match(r);
            console.log(val);
            if(val){
                return val[1];
            }else{
                return "";
            };
        };*/

    function getCookie(key){
        var val = document.cookie.match("\\b" + key + "=([^;]*)(;|$)" );
        console.log(val);
        return val?val[1]:"";

    };
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
    </style>
</head>
<body>
    <div id="box">
        <p id="p1"></p>
        <p id="p2"></p>
    </div>

    <script>
        var op1 = document.getElementById("p1"),
            op2 = document.getElementById("p2"),
            val = getCookie("Time");
        if(val){
            op1.innerHTML = "您上次访问的时间是:"+ val;
        }else{
            op1.innerHTML = "欢迎第一次访问本网站";
        };

        var date = new Date(),
            YY = date.getFullYear(),
            MM = date.getMonth(),
            DD = date.getDate(),
            hh = date.getHours(),
            mm = date.getMinutes(),
            ss = date.getSeconds(),
            time = YY + "年" + MM + "月" + DD + "日" + hh + "点" + mm + "分" + ss + "秒";
        op2.innerHTML = "当前时间是: " + time;

        setCookie({
            Time : time
        },2);//天数
        //获取cookie
        function getCookie(key){
            var val = document.cookie.match("\\b" + key + "=([^;]*)(;|$)" );
            return val?val[1]:"";
        };

        //设置cookie
        function setCookie(mJson , time){
            var data = new Date(new Date().getTime() + time*24*60*60*1000).toGMTString();
            for(var key in mJson){
                document.cookie = key+"="+mJson[key]+";expires="+data;
            };

        };
        removeCookie("Time");
        //移除cookie
        function removeCookie(key){
            var json = {};
            json[key] = "";
            setCookie(json,-1)
        };
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值