在項目中用到cookie一般是用在註冊時的記住賬號密碼或保存固定時間的數據 // cookie 存儲setCookie(c_name, c_pwd, exdays) { // 設置存儲用戶名密碼 var exdate = new Date(); exdate.setTime(exdate.getTi ...
在項目中用到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); }