客户端对cookie的操作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
![](/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cookies Example</title>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure)
{
var sCookie = sName + "=" + encodeURIComponent(sValue);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oExpires)
{
sCookie += "; expires=" + oExpires.toGMTString();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sPath)
{
sCookie += "; path=" + sPath;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sDomain)
{
sCookie += "; domain=" + sDomain;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (bSecure)
{
sCookie += "; secure";
}
document.cookie = sCookie;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function getCookie(sName)
{
var sRE = "(?:; )?" + sName + "=([^;]*);?";
var oRE = new RegExp(sRE);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oRE.test(document.cookie))
{
return decodeURIComponent(RegExp["$1"]);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{return null;}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function deleteCookie(sName, sPath, sDomain)
{
var sCookie = sName + "=; expires=" + (new Date(0)).toGMTString();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sPath)
{
sCookie += "; path=" + sPath;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sDomain)
{
sCookie += "; domain=" + sDomain;
}
document.cookie = sCookie;
}
</script>
</head>
<body>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
alert("Setting cookies
");
setCookie("name", "Nicholas");
setCookie("book", "Professional JavaScript",new Date(Date.parse("Jan 28, 2008")));
setCookie("message", "Hello Cookie", new Date(Date.parse("Jan 28, 2008")), "/books", "http://www.cnblogs.com/sw22225458", true);
alert("The value of cookie 'name' is " + getCookie("name"));
alert("The value of cookie 'book' is " + getCookie("book"));
alert("The value of cookie 'message' is " + getCookie("message"));
alert("Deleting cookies
");
deleteCookie("name");
deleteCookie("book");
deleteCookie("message");
alert("The value of cookie 'name' is " + getCookie("name"));
alert("The value of cookie 'book' is " + getCookie("book"));
alert("The value of cookie 'message' is " + getCookie("message"));
</script>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)