论坛首页 Web前端技术论坛

JS中对Cookie的操作

浏览 2474 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-31  
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
    var prefix = name + "="
    var start = document.cookie.indexOf(prefix)

    if (start==-1) {
        return null;
    }
   
    var end = document.cookie.indexOf(";", start+prefix.length)
    if (end==-1) {
        end=document.cookie.length;
    }

    var value=document.cookie.substring(start+prefix.length, end)
    return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
   发表时间:2011-08-31  

/**
 * @class x.util.Cookie
 * Cookie操作类
 * 这个类是一个静态类,不需要实例化。
 */

x.util.Cookie = (function(){
	function set(name, value, expires, domain, path, secure){
		if(!expires){
			expires = new Date();
			expires.setTime(new Date().getTime() + 1 * (24 * 60 * 60 * 1000));
		}		
		var s = name + '=' + encodeURIComponent(value) +
		        ';expires=' + expires.toGMTString() + 
		        ';path=' + (path ? path : '/');
		             
		if(domain)
			s += ';domain=' + domain;		
		if(secure)		
			s += ';secure';		
		document.cookie = s;	
	}
	
	function get(name){		
		var r = new RegExp('(?:;)?' + name + '=([^;]*);?');
		r.test(document.cookie);
		return decodeURIComponent(RegExp.$1);
	}
	
	function remove(name, domain, path){
		var expire = new Date();
		expire.setTime(0); 
 		var s = name + '=;expires=Thu, 01-Jan-2010 00:00:01 GMT' + 
		        ';path=' + (path ? path : '/');
		
		if(domain)
			s += ';domain=' + domain;
		 console.log(s)	
		document.cookie = s;
	}
	
	return {
		set: set,
		get: get,
		remove: remove
	};
})();
 
0 请登录后投票
   发表时间:2012-04-07  
var CookieUtil = {
get: function(name) {
var cookie = document.cookie;
var cookieName = encodeURIComponent(name) + "=";
var start = cookie.indexOf(cookieName);
var value = null;
if (start > -1)
{
var end = cookie.indexOf(";", start);
if (end == -1)
end = cookie.length;
value = decodeURIComponent(cookie.substring(start + cookieName.length, end));
}
return value;
},
set: function(name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
if (expires instanceof Date)
cookieText += "; expires=" + expires.toGMTString();
if (path)
cookieText += "; path=" + path;
if (domain)
cookieText += "; domain=" + domain;
if (secure)
cookieText += "; secure";

document.cookie = cookieText;
},
unset: function(name, path, domain, secure) {
this.set(name, "", new Date(0), path, domain, secure);
}
};
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics