开发学院

您的位置:首页>教程>正文

教程正文

ES6 Cookies

ES6 Cookies

  Web浏览器和服务器使用http协议进行通信,但是http是无状态协议,也就是说,服务器不会在请求中维护客户端的数据。客户端与服务器之间的完整请求响应周期被定义为会话。cookie是浏览器用来存储与用户会话相关的数据的默认机制。

它是怎么工作的?

  服务器以cookie的形式向访问者的浏览器发送一些数据,如果浏览器可以接受cookie,它将这些数据以纯文本的形式记录存储在访问者的硬盘上。现在,当访问者到达同一站点上的其他页面时,浏览器将cookie发送到服务器进行检索。如果检索到,那么服务器就知道早期存储过什么,就可以从中取值了。

cookie数据包含有5个字段。

  •   Expires:cookie的过期日期。如果这是空白,cookie将在访问者退出浏览器时过期。

  •   Domain:cookie作用的域名。

  •   Path:设置cookie的目录或网页的路径。

  •   Secure:如果此字段包含“secure”一词,则只能使用安全服务器(https协议)检索cookie。如果此字段为空白,则不存在此类限制。

  •   Name = Value:cookie以键值对的形式保存。

  Cookie最初是为CGI编程而设计的。Cookie中包含的数据会在web浏览器和web服务器之间自动传输,因此服务器上的CGI脚本可以读取和写入存储在客户端的Cookie值。

  JavaScript还可以使用文档对象的Cookie属性来操作CookieJavaScript可以读取、创建、修改和删除应用于当前网页的Cookie.

存储Cookie

  创建Cookie的最简单方法是向document.cookie对象分配一个字符串值,该对象看起来像这样。

"document.cookie = "key1 = value1; key2 = value2; expires = date";

  expires属性是可选的。如果您提供此属性的有效日期或时间,则该cookie将在给定日期或时间到期,此后,cookie的值将无法访问。

  注意-Cookie值不能包含分号、逗号或空格,如果您希望保存这些符号,需要使用JavaScript的escape()函数在将值存储在cookie中之前对其进行编码。如果您这样做则需要在读取cookie值的时候,使用相应的unescape()函数来解码。

例子

<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" onclick = "WriteCookie();"/> 
      </form> 
   </body> 
</html>

读取Cookie

  读取Cookie与存储Cookie一样简单。当您想访问cookie时,直接取document.cookie就可以,使用split()函数将得到的字符串拆分为键和值,如下例所示。

例子

<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>

设置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 = "formname" action = ""> 
         Enter Cookie 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 = "formname" action = ""> 
         Enter Cookie Name: <input type = "text" name = "customer"/> 
         <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> 
      </form> 
   </body> 
</html>