web 用户存储用户信息cookie, cookie的设置,cookie的获取,cookie的移除

Posted fishstudy520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web 用户存储用户信息cookie, cookie的设置,cookie的获取,cookie的移除相关的知识,希望对你有一定的参考价值。

web 用户存储用户信息cookie, cookie的设置,cookie的获取,cookie的移除


设置cookie 值  该函数设置了 cookie 名、cookie 值、cookie过期时间。

function setCookie(name,value,day) {
    var oDate = new Date();
    var d = oDate.setDate(oDate.getDate()+day);//设置从当前时间几天后过期
    var expires = ‘expires=‘+ oDate;
    document.cookie = name+"="+value+";"+expires
};

获取cookie  获取指定 cookie 的值

function getCookie(name) {
      var strCookie = document.cookie;//获取所有的cookie值
      var oArr = strCookie.split(‘;‘);
      for(var i=0;i<oArr.length;i++){
          var c = oArr[i].trim();
          var oArr2 = c.split(‘=‘);
          if(oArr2[0].indexOf(name)>-1) {
               return oArr2[1];
          }
      }
      return ‘‘;
}

 

移除Cookie值

function removeCookie(name){
    // 将cookie的过期时间设置成过去时间,可以将cookie 移除
    setCookie(name,‘‘,-1);
}

  setCookie(‘name‘,‘czy‘,1);
  setCookie(‘age‘,18,2);
  console.log(getCookie(‘name‘));//czy
  // debugger;
  removeCookie(‘name‘);
  console.log(getCookie(‘name‘)); // ‘‘

 完整案例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <style>
        input{
            outline: none;
        }
        a{
            color: #333;
            cursor: pointer;
        }
    </style>
    <body>
        <form id="form">
            名字:
            <input type="text" name="name" id=‘name‘>
            密码:
            <input type="password" name="password" id="password">
            <input value="提交" type="button" id="btn" />
            <input value="清除Cookie" type="button" id="clear" />
        </form>
        <script>
            // 设置cookie
            function setCookie(name,value,day) {
                var oDate = new Date();
                var d = oDate.setDate(oDate.getDate()+day);
                var expires = expires=+ oDate;
                document.cookie = name+"="+value+";"+expires
            };
            // 获取cookie
            function getCookie(name) {
                var strCookie = document.cookie;//获取所有的cookie值
                var oArr = strCookie.split(;);
                for(var i=0;i<oArr.length;i++){
                    var c = oArr[i].trim();
                    var oArr2 = c.split(=);
                    if(oArr2[0].indexOf(name)>-1) {
                        return oArr2[1];
                    }
                }
                return ‘‘;
            }
            // 移除cookie
            function removeCookie(name){
                // 将cookie的过期时间设置成过去时间,可以将cookie 移除
                setCookie(name,‘‘,-1);
            }
            
            function getElementById(id) {
                return document.getElementById(id);
            }
            window.onload = function() {
                var submitBtn = getElementById(btn);
                var clearBtn = getElementById(clear);
                var oName = getElementById(name);
                var oPassword = getElementById(password);
                oName.value = getCookie(uname);
                oPassword.value = getCookie(password);
                submitBtn.onclick = function(){
                    setCookie(uname,oName.value,5);
                    setCookie(password,oPassword.value,5);
                };
                clearBtn.onclick = function () {
                    removeCookie(uname);
                    removeCookie(password);
                    oName.value = ‘‘;
                    oPassword.value = ‘‘;
                }
            }
            
        </script>
    </body>
</html>

 

以上是关于web 用户存储用户信息cookie, cookie的设置,cookie的获取,cookie的移除的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript Cookie

JavaScript相关, Cookie

JavaScript相关, Cookie

cookie 怎样存储值?

cookie储存

cookie解析