在專案中用到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.uj5u.com/qiye/117321.html
標籤:JavaScript
上一篇:JS事件總結
下一篇:nodeErr - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
