javascirpt Cookie机制

var Cookie = {
	setCookie:function(name,value,opts){
		if(value !== undefined){
			var expires = "",
				arr = [];
			opts = opts || {};
			if(value === null){
				value = "";
				expires = -1;
			}
			arr.push(encodeURIComponent(name) + " = " + encodeURIComponet(value));
			if(opts.expires && opts.expires instanceof Date){
				arr.push("expires = "+ opts.expires.toUTCString());
			}
			arr.push(opts.path ? "path = "+opts.path : "");
			arr.push(opts.domain ? "domain = "+opts.domain : "");
			arr.push(opts.secure ? "secure"+opts.secure : "");
			document.cookie = arr.join("; ");
		}
	},
	getCookie:function(name){
		var dck = document.cookie,
			cookieVal = null,
			cname = decodeURIComponent(name) + " =",
			start = dck.indexOf(cname);
		if(start > -1){
			var end = document.cookie.indexOf(";",start);
			if(end == -1){
				end = dck.length;
			}
			cvalue = decodeURIComponent(dck.substring(start + cname.length , end));
		}
		return cvalue;	
	},
	removeCookie:function(name,opts){
		opts.expires = new Date(0);
		Cookie.setCookie(name,"",opts);
	}
}

你可能感兴趣的:(javascirpt Cookie机制)