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>
  • 相关阅读:
    SQL exist
    Mac 加密软件
    appcon 图标打包
    启动页面
    获取第三方的授权
    实现一个简单但是好看的滑动栏
    实现聊天界面的代码
    简单的设置圆形按钮
    自己搭建服务端前透析
    A1056Mice and Rice (25分)
  • 原文地址:https://www.cnblogs.com/shoestrong/p/5170515.html
Copyright © 2011-2022 走看看