码迷,mamicode.com
首页 > Web开发 > 详细

js cookie操作

时间:2016-04-29 14:40:27      阅读:134      评论:0      收藏: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;
}

  

js cookie操作

标签:

原文地址:http://www.cnblogs.com/hyh123/p/5445830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!