原生JS操作cookie


//cookie


function setCookie(c_name, value, expiredays){  
     var exdate=new Date();  
    exdate.setDate(exdate.getDate() + expiredays);  
    document.cookie=c_name+ "=" + value + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/;domain="+cookieDomain;+";path=/;domain="+cookieDomain; 
   }  
   
 //讀取cookie
 function getCookie(name){
     var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
     if(arr = document.cookie.match(reg)){
         return (arr[2]);     
     }else{
         return null;     
     }
 };


 function delCookie(name){
     var exp = new Date();
     exp.setTime(exp.getTime() - 1);
     var cval = getCookie(name);
     if(cval != null){
         document.cookie= name + "="+cval+";expires=" + exp.toGMTString()+";path=/;domain="+cookieDomain;
     }
 };



//cookie



if( !(getCookie('cookie_flag')) ){debugger;
     delCookie('atgregion');
     setCookie('cookie_flag',1,30);
}

 


免責聲明!

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



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