zoukankan      html  css  js  c++  java
  • cookie的设置、获取和删除封装

    在我们为了去完成数据储存,有时会用到cookie,简单封装一下cookie:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    <script type="text/javascript">
        //设置cookie
        function setCookie(name,value,days){
            days=days?days:30;//一个月后失效
            var exp=new Date();
            exp.setTime(exp*1+days*24*60*60*1000);
            document.cookie=name+"="+escape(value)+";expires="+exp;
        }
        setCookie("tel","13546",10);
        setCookie("name","arch",1);
        console.log(document.cookie);
    
        //获取cookie
        function getCookie(name){
            var reg=new RegExp('(^| )'+name+'=([^;]*)(;|$)');
            var arr=document.cookie.match(reg);
            if(arr) return arr[2];
            return null;
        }
        getCookie("name");
        console.log(getCookie("name"));
    
        //删除cookie
        function delCookie(name){
            if(getCookie(name)){
                setCookie(name,"",-1);
            }
        }
        delCookie("tel");
    </script>
    </body>
    </html>
  • 相关阅读:
    shell的格式化输出命令printf
    shell数组
    shell字符串
    shell注释
    shell运算符
    shell替换
    shell特殊变量
    shell变量
    linux修改主机名
    ssh免密码登录设置
  • 原文地址:https://www.cnblogs.com/shoestrong/p/5170515.html
Copyright © 2011-2022 走看看