cookie笔记(二)

  • 小荔枝
<form action="javascript:void(0)" method="get" accept-charset="utf-8">
    <label>
        账户:
        <input type="text" name="" placeholder="用户" id="uid">
    </label>
    <label>
        密码:
        <input type="password" name="" placeholder="密码" id="pasw">
    </label>
    <label>
        提交:
        <input type="button" name="" value="提交" id="btn">
    </label>
     <label>
        清除:
        <input type="button" name="" value="清除" id="remov">
    </label>
</form>
  1. 设置添加
 1 //封装成插件,多复用。
 2 function setCookie(name,value,iDay){ //设置添加
 3         var oDate=new Date();
 4         oDate.setDate(oDate.getDate()+iDay);
 5         document.cookie=name+"="+value+";expires="+oDate;
 6 }
 7 
 8 document.getElementById("btn").onclick=function(){
 9             var uid=document.getElementById("uid").value;
10             var pasw=document.getElementById("pasw").value;
11             setCookie("uid",uid,5);
12             setCookie("pasw",pasw,5);
13         }

  2.删除记录

1 function remoCookie(name) //删除记录
2     {
3         setCookie(name,"1",-1);
4     }
5 
6 document.getElementById("remov").onclick=function(){
7             remoCookie("uid")
8             remoCookie("pasw")
9         }

  3.获取记录

 1 function getCookie(name){ //查询遍历
 2         var arr=document.cookie.split(";");
 3         for(var i=0;i<arr.length;i++){
 4             var arr2=arr[i].split("=");
 5             if(arr2[0]==name){
 6                  return arr2[1]
 7             }
 8         }
 9         return ""
10  }
11 
12 //我们可以让页面一加载完毕就给容器赋值,如果有的话。
13 window.onload=function(){
14             document.getElementById("uid").value=getCookie("uid");
15             document.getElementById("pasw").value=getCookie("pasw");
16         }

  4.修改cookie

只要重新输入账户密码,就可以完成修改。

原文地址:https://www.cnblogs.com/webSong/p/7002446.html