js cookie片段记录

记录一个cookie操作片段;

//cookie  Set-Cookie 头中设置 name = value expire (格林威治时间)  secure 字段规定只有通过SSL链接才能传输

var CookieUtil = {
	set:function(name, value, expire, domain, path, secure){
		var cookieText =  encodeURIComponent(name) + "=" + encodeURIComponent(value);
		if (expire && expire instanceof Date) {
			cookieText += "; expires=" + expire.toGMTString();
		};
		if (path) {
			cookieText += "; path=" + path;
		};
		if (domain) {
			cookieText += "; domain=" + path;
		};
		if (secure) {
			cookieText += "; secure";
		};
		document.cookie = cookieText;
	},
	get:function(name){
		var cookieName = encodeURIComponent(name), 
			startIndex = document.cookie.indexOf(cookieName),
			cookieValue = null;
		if (startIndex > -1) {
			var endIndex = document.cookie.indexOf(";", startIndex);
			if (endIndex == -1) {
				endIndex = document.cookie.length;
			}
			cookieValue = document.cookie.substring(startIndex + cookieName.length + 1, endIndex);
		};
		return cookieValue;
	},
	unset:function(name , path, domain, secure){
		this.set(name, "", new Date(0), domain, path, secure);
	}
}


你可能感兴趣的:(js cookie片段记录)