Web浏览器和服务器使用HTTP协议进行通信,而HTTP是无状态协议,但是对于商业网站,需要在不同页面之间维护会话信息,这时可以使用Cookie解决这个问题。
您的服务器以cookie的形式向访问者的浏览器发送一些数据,浏览器可以接受cookie。如果是这样,它将以纯文本记录的形式存储在访问者的硬盘上。现在当访问者到达您网站上的另一个页面时,浏览器会将相同的cookie发送到服务器以进行检查。一旦检查到,您的服务器就会知道您以前存储的内容。
Cookie是5个可变长度字段的参数-
Expires - 到期的日期。如果为空,则cookie将在访问者退出浏览器时过期。
Domain - 您站点的域名。
Path - 设置cookie的目录或网页的路径。如果要从任何目录或页面检索cookie,则可能为空。
Secure - 如果此字段包含"Secure"一词,则只能使用安全服务器检索cookie。如果此字段为空,则不存在此类限制。
Name=Value - 以键值对的形式设置和检索Cookie
Cookie最初是为CGI编程设计的, Cookie中包含的数据会在Web浏览器和Web服务器之间自动传输,因此服务器上的CGI脚本可以读取和写入存储在客户端上的Cookie值。
JavaScript还可以使用 Document 对象的 cookie 属性来操作cookie, JavaScript可以读取,创建,修改和删除适用于当前网页的cookie。
储存Cookie
创建cookie的最简单方法是将一个字符串值分配给document.cookie对象,如下所示。
document.cookie="key1=value1;key2=value2;expires=date";
这里的 expires 属性是可选的,如果为该属性提供有效的日期或时间,则cookie将在给定的日期或时间到期,此后,将无法访问cookie的值。
注意 - Cookie值不能包含分号,逗号或空格。因此,您可能要使用JavaScript escape()函数对值进行编码,然后再将其存储在cookie中。如果这样做,则在读取Cookie值时还必须使用相应的 unescape()函数。
请尝试以下方法。它在输入cookie中设置客户名称。
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie() {
if( document.myform.customer.value == "" ) {
alert("Enter some value!");
return;
}
cookievalue=escape(document.myform.customer.value) + ";";
document.cookie="name=" + cookievalue;
document.write ("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form>
</body></html>运行上面代码输出
现在,您的计算机有了一个名为 name 的cookie。您可以使用多个用逗号分隔的Name=Value对来设置多个Cookie。
读取Cookie
读取cookie就像编写cookie一样简单,因为document.cookie对象的值就是cookie。 document.cookie字符串将保留由分号分隔的name=value对的列表,其中 name 是cookie的名称,而value是其字符串值。
您可以使用字符串的 split()函数将字符串分成键和值,如下所示:
请尝试以下示例获取所有cookie。
<html>
<head>
<script type="text/javascript">
<!--
function ReadCookie() {
var allcookies=document.cookie;
document.write ("All Cookies : " + allcookies );
//Get all the cookies pairs in an array
cookiearray=allcookies.split(';');
//Now take key value pair out of this array
for(var i=0; i<cookiearray.length; i++) {
name=cookiearray[i].split('=')[0];
value=cookiearray[i].split('=')[1];
document.write ("Key is : " + name + " and Value is : " + value);
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
<p> click the following button and see the result:</p>
<input type="button" value="Get Cookie" onclick="ReadCookie()"/>
</form>
</body></html>运行上面代码输出
注意 - 这里的 length 是 Array 类的方法,该方法返回数组的长度,无涯教程将在单独的章节中讨论数组。
注意 - 您的计算机上可能已经设置了其他cookie。上面的代码将显示您计算机上设置的所有cookie。
设置有效时间
您可以通过设置过期日期保存在cookie中,从而将cookie的存活更长时间,可以通过将"Expire" 属性设置为日期和时间来完成。
请尝试以下示例,它说明了如何将Cookie的有效期延长1个月。
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie() {
var now=new Date();
now.setMonth( now.getMonth() + 1 );
cookievalue=escape(document.myform.customer.value) + ";"
document.cookie="name=" + cookievalue;
document.cookie="expires=" + now.toUTCString() + ";"
document.write ("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie()"/>
</form>
</body></html>运行上面代码输出
删除Cookie
有时您会想要删除cookie,以便后续尝试读取cookie不会返回任何内容。为此您只需要将到期日期设置为过去的某个时间。
请尝试以下示例。它说明了如何通过将cookie的过期日期设置为当前日期后一个月来删除它。
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie() {
var now=new Date();
now.setMonth( now.getMonth() - 1 );
cookievalue=escape(document.myform.customer.value) + ";"
document.cookie="name=" + cookievalue;
document.cookie="expires=" + now.toUTCString() + ";"
document.write("Setting Cookies : " + "name=" + cookievalue );
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie()"/>
</form>
</body></html>运行上面代码输出
随时随地看视频