js的cookie寫入存儲與讀取


js的cookie寫入存儲與讀取

在路徑url截取需要的數據,存儲到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';
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM