js的cookie寫入存儲與讀取

js的cookie寫入存儲與讀取html

在路徑url截取須要的數據,存儲到cookie裏,讀取成功並實現跳轉。cookie

//寫cookies 過時時間 2小時後
 function setCookie(c_name, value, expiretimes){     var exdate=new Date(); exdate.setTime(exdate.getTime()+(expiretimes*60*60*1000)); console.log(exdate); //Tue Nov 05 2019 21:21:26 GMT+0800 (中國標準時間)
        console.log(exdate.toGMTString());   //Tue, 05 Nov 2019 13:21:26 GMT //相差8小時
     document.cookie=c_name+ "=" + escape(value) + ";path=/" + ((expiretimes==null) ? "" : ";expires="+exdate.toGMTString());   } 
//讀取cookies 
 function getCookie(name){ var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return (arr[2]); else  
            return null; }
//截取url字符AUTH_TICKET /list.html?AUTH_TICKET=8977656
 function getQueryString (name){ var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }
setCookie('AUTH_TICKET',getQueryString("AUTH_TICKET"),10); if(getCookie("AUTH_TICKET") && getCookie("AUTH_TICKET") != null){ window.location.href = '/index.html'; }

 

相關文章
相關標籤/搜索