JS 两种方法存和取cookies

//第一种方法
function setCookie(name,value)
{
  var Days = 30; //此 cookie 将被保存 30 天
  var exp  = new Date();    //new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = name + "="+ escape(value) +";path=/;expires="+ exp.toGMTString();
    $("#userIntro").css("display","none");
}

function getCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}


********************************************
第二种方法
 function getcookie(name){ 
var cookie_start = document.cookie.indexOf(name); 
var cookie_end = document.cookie.indexOf(";", cookie_start); 
cookievalue = cookie_start == -1 ? '' :
unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
return cookievalue; 
} 

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
 var expires = new Date(); expires.setTime(expires.getTime() + seconds);
 document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTString() : '') + (path ? '; path=' + path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); }

你可能感兴趣的:(Cookies)