博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

js cookie操作

Posted on 2016-04-29 12:02  红与黑hyh  阅读(176)  评论(0编辑  收藏  举报

 

//写Cookie
function writeCookie(name, value)
{
	var expire = new Date();
	expire.setFullYear(expire.getFullYear() + 20);
	expire = '; expires=' + expire.toGMTString();
	document.cookie = name + '=' + escape(value) + expire;
}

  

// 读取Cookie
function readCookie(name)
{
	var cookieValue = '';
	var search = name + '=';
	if (document.cookie.length > 0)
	{
		var offset = document.cookie.indexOf(search)
		if (offset != -1)
		{
			offset += search.length;
			var end = document.cookie.indexOf(';', offset);
			if (end == -1) end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

  

// 读取Cookie
function readCookieCn(name)
{
	var cookieValue = '';
	var search = name + '=';
	if (document.cookie.length > 0)
	{
		var offset = document.cookie.indexOf(search)
		if (offset != -1)
		{
			offset += search.length;
			var end = document.cookie.indexOf(';', offset);
			if (end == -1) end = document.cookie.length;
			cookieValue = decodeURI(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}