js操作cookie(转载)
转载自http://www.cnblogs.com/5tao/archive/2009/03/25/1421833.html
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookie.aspx.cs" Inherits="cookie" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
/*
功能:保存cookies函数
参数:name,cookie名字;value,值
*/
function SetCookie(name,value){
var Days = 60; //cookie 将被保存两个月
var exp = new Date(); //获得当前时间
exp.setTime(exp.getTime() + Days*24*60*60*1000); //换成毫秒
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
/*
功能:获取cookies函数
参数:name,cookie名字
*/
function getCookie(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null)
return unescape(arr[2]);
return null;
}
/*
功能:删除cookies函数
参数:name,cookie名字
*/
function delCookie(name){
var exp = new Date(); //当前时间
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function addcookie()
{
SetCookie("myname","wutao");
}
function readcookie()
{
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick="SetCookie('myname','wutao')">加入cookie</a><br />
<a href="#" onclick="alert(getCookie('myname'))">读取cookie</a>
</div>
</form>
</body>
</html>