JavaScript和Cookies
Web浏览器和服务器使用HTTP协议进行通信,而HTTP是无状态协议。但是对于商业网站,需要在不同页面之间维护会话信息。例如,一个用户注册在完成许多页面后结束。但是如何在所有网页上维护用户的会话信息。
在许多情况下,使用Cookie是记住和跟踪偏好,购买,佣金和其他信息(以获得更好的访问者体验或站点统计信息所需)的最有效方法。
这个怎么运作 ?
你的服务器以cookie的形式向访问者的浏览器发送一些数据。浏览器可以接受该cookie。如果是这样,它将以纯文本记录的形式存储在访问者的硬盘上。现在,当访问者到达你网站上的另一个页面时,浏览器会将相同的cookie发送到服务器以进行检索。一旦检索到,你的服务器就会知道/记住以前存储的内容。
Cookies是5个可变长度字段的纯文本数据记录:
过期时间(Expires):Cookie到期的日期。如果为空,则cookie将在访问者退出浏览器时过期。
域名(Domain):你网站的域名。
路径(Path):设置cookie的目录或网页的路径。如果要从任何目录或页面检索cookie,则该字段可以为空白。
安全(Secure):如果此字段包含“安全”一词,则只能使用安全服务器检索cookie。如果此字段为空白,则不存在此类限制。
name=value:Cookie以键值对的形式设置和检索
Cookies最初是为CGI编程而设计的。 Cookie中包含的数据会在Web浏览器和Web服务器之间自动传输,因此服务器上的CGI脚本可以读取和写入存储在客户端上的Cookie值。
JavaScript也可以使用cookie的属性Document目的。 JavaScript可以读取、创建、修改和删除适用于当前网页的cookie。
储存Cookies
创建cookie的最简单方法是将一个字符串值分配给document.cookie对象,如下所示。
document.cookie = "key1 = value1;key2 = value2;expires = date";
expires属性是可选的。如果为该属性提供有效的日期或时间,则cookie将在给定的日期或时间到期,此后,将无法访问cookie的值。
注意:Cookie值不能包含分号,逗号或空格。因此,你可能要使用JavaScriptescape()函数将值存储在cookie中之前对其进行编码。如果执行此操作,则还必须使用相应的unescape()读取Cookie值时的功能。
尝试以下方法。它在输入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。
读取Cookies
读取cookie就像编写cookie一样简单,因为document.cookie对象的值就是cookie。因此,只要你想访问cookie,就可以使用此字符串。 document.cookie字符串将保留由分号分隔的“name=value”对的列表,其中name是Cookie的名称,值是其字符串值。
你可以使用字符串”split()用于将字符串分解为键和值的函数,如下所示:
请尝试以下示例获取所有cookie。
<html> <head> <script type = "text/javascript"> <!-- function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); //获取数组中的所有cookie对 cookiearray = allcookies.split(';'); //现在从该数组中取出键值对 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中,从而将cookie的寿命延长到当前浏览器会话之外。可以通过设置“expires”属性归于日期和时间。
请尝试以下示例。它说明了如何将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>