js的cookie操作类

使用起来很方便的

代码量特别小

//cookie操作类
function store(name, value, expires, path, domain, secure) {
	if (value === undefined || value === null) {
		var val = get(name);
		if (value === null) store(name, '', -10);
		try {
			return JSON.parse(val)
		} catch(e) {
			return val
		}
	} else {
		if (typeof(value) == 'object') value = JSON.stringify(value);
		document.cookie = name + '=' + encodeURIComponent(value) + exp(expires) + (path ? ';path=' + path: ';path=/') + (domain ? '; domain=' + domain: '') + (secure ? '; secure': '') + ';'
	}
	function exp(s) {
		D = new Date(),
		D.setTime(D.getTime() + (s === undefined ? 2592000 : s ) * 1000);
		return ';expires=' + D.toUTCString()
	}
	function get(name) {
		if (document.cookie.length > 0) {
			start = document.cookie.indexOf(name + "=");
			if (start != -1) {
				start = start + name.length + 1;
				end = document.cookie.indexOf(";", start);
				if (end == -1) end = document.cookie.length;
				return decodeURIComponent(document.cookie.substring(start, end))
			}
		};
		return null
	}
}

原文链接:http://www.fastmvc.com/blog/1249.html