js 操作cookie 添加 删除 获取

function   setCookie(name,value) {  
		var never = new Date();
		//设置Cookie的有效期
		never.setTime(never.getTime() + 1*60*60*1000); 
		var expString = "expires=" + never.toGMTString() +";";
		document.cookie = name+"=" + escape(value) +"; " + expString;
		
 }   
function   getCookie(name) {  
	var result = null;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1) 
	{

		startOfCookie += searchName.length; 
		endOfCookie = myCookie.indexOf(";", startOfCookie);
		result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
	
  }
function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
	   var date = new Date();
	   date.setTime(date.getTime() - 10000);
	   document.cookie = name + "=a; expires=" + date.toGMTString();
}

你可能感兴趣的:(cookie)