cookie 在登录时的存储,获取,清除

 在项目中用到cookie一般是用在注册时的记住账号密码或保存固定时间的数据

// cookie 存储
setCookie(c_name, c_pwd, exdays) { // 设置存储用户名密码 var exdate = new Date(); exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); // cookie 有效期 window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString(); window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString(); },
// cookie 获取 getCookie: function() { // 从cookie 获取用户名密码 if (document.cookie.length > 0) { var arr = document.cookie.split("; "); // 获取cookie 后以 "; " 进行分割 for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split("="); // 以 "=" 来进行分割 if (arr2[0] == "userName") { // 判断用户名是否是第一个 this.phone = arr2[1]; } else if (arr2[0] == "userPwd") { this.password = arr2[1]; } } this.check = true; // 记住密码单选框 } },
// cookie 清除 clearCookie: function() { this.setCookie("", "", -1); }

 

原文地址:https://www.cnblogs.com/mingyeliu/p/12584139.html