The use of cookies in JS

Source: Internet
Author: User

Find the information on the Internet, collect

1 functiongetcookies (name)2 {3 vararr = Document.cookie.match (NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));4 if(Arr! =NULL)returnUnescape (arr[2]);return‘‘;5 }6 functionSetcookie (name, value, expires, path, domain, secure)7 {8 varLivedate =NewDate ();9expires = Livedate.settime (Livedate.gettime () + expires*60*1000);//millisecondsTen //expires = new Date (new Date ()). GetTime () + expires * 60000);//per minute OneDocument.cookie = name + "=" + Escape (value) + A((expires)? "; Expires= "+ Expires:" ") + -(path)? "; Path= "+ Path:" ") + -(domain)? "; domain= "+ domain:" ") + the(secure)? "; Secure ":" "); -}
以下网上找的资料: ————————————————— js cookie总结                                                         最近需要用FSO操作文件,有这样一个需求,用js操纵cookie保存用户上次一打开文件的路径,发现用js操作cookie和用服务器语言操作 cookie有一些差异,还有很多小的细节需要注意,如果运用不得当会引发很多不可预料的结果。当我遇到问题的时候在网上查了很多这方面的资料,发现好多都是简单以理论的方式介绍了js如何操作cookie。但我照着做却发现有些地方根本就不是这么回事,下面我总结了我在用js操作cookie的一些经验。 用js操纵cookie是通过document对象下的cookie对象,其实document.cookie就是字符串, 所以我们使用它就像使用字符串一样,可以使用字符串的所有方法,只不过这个字符串需要有个格式(key=value),设置cookie的示例代码如下
Document.cookie= "Key=escape (value)";
   cookie的值不能使用分号(;)、逗号(,)、等号(=)以及空格。在cookie的名中做到这点很容易,但要保存的值是不确定的。如何来存储这些值呢?方法是用escape()函数进行编码,它能将一些特殊符号使用十六进制表示,例如空格将会编码为“20%”,从而可以存储于cookie值中,而且使用此种方案还可以避免中文乱码的出现。value上使用了escape方法。在取值的时候需要unescape(value)对value再进行转码即可。If you want to set up multiple cookies, you need to use this method multiple times. The correct setting method is:
Document.cookie= "Key=escape (value)";d ocument.cookie= "Key1=escape (value1)"

Instead of

Document.cookie= "Key=escape (value); Key1=escape (value1)";
如果想取出cookie的值,可以直接调用document.cookie获得,如果有多个值,多个值用分号(;)分隔,每个值用等号(=)分隔,我们可以对cookie先按照分号(;)进行分隔(split),然后再按等号(=)分隔(split)。然后循环比较key的值,如果key相等,则取出 value。需要注意一点,如果有多个值,第二个值的key值前面多一个空格,需要去除。下面我给出获取cookie的示例代码
1  functionGetCookie (key) {2 varAcookie = Document.cookie.split (";");3  for(vari=0; i < acookie.length; i++){  4 varAcrumb = Acookie[i].split ("="); 5 if(key = = = Acrumb[0].replace (/^\s*|\s*$/, ""))){   6 returnUnescape (acrumb[1]);7 }  8 } 9}
  经过前面的示例代码在一个页面设置cookie后在这个页面也能取到,但是cookie存在哪里了。通常情况下,cookie会存放在 C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files目录下。但是我们删除了这个文件夹下的所有文件后,再获得cookie,还能够访问到。关闭浏览器后,再次打开浏览器后还能获得到 cookie。那cookie的默认生存期是多久呢。怎么才能清除cookie呢。非常抱歉,我也不知道存哪了,但是只要注销或重启之后,设置的 cookie将销毁。我个人认为这是设计js的cookie的一个bug.因为用户不能够通过一种方式及时清除cookie是非常不友好的。我们可以通过设置cookie时可以传递一个属性expires,该属性的作用是设置cookie的生存期。设置cookie的生存期的示例代码如下:
1  var New Date (); 2 livedate.settime (livedate.gettime () + 3*24*60*60*1000); 3 document.cookie= "name=test;expires=" + livedate.togmtstring ();
上面代码设置cookie的name的存活时间为3天。删除cookie的值就是设置expires一个过期的时间即可,示例代码如下
1 var New Date (); 2 livedate.settime (Livedate.gettime ()-10000); 3 document.cookie = "name=test;expires=" + date.togmtstring ();
但是有趣的是,设置了expires属性后,我们在C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files目录下发现有存储cookie的文件。此时我们删除该文件后,发现设置的cookie确实销毁了。这正符合我们的要求。所以建议用js设置 cookie的时候一定要设置expires属性,正常来说我们要用到cookie的时候就应该根据需求明确设计cookie存活多久。 下面我们再说一下js操作cookie的作用域。默认情况下js操作cookie的作用域是目录级的,也就是在当前目录下设置的cookie,当前目录及该目录下的所有子目录下的所有文件都能够访问该cookie,例如在http: //localhost:8090/APPTest/aaa/testCookie.html中设置的cookie,在http://localhost:8090/APPTest/aaa/目录下的所有文件和http://localhost:8090/APPTest/aaa/bbb/下的所有文件都能够访问到这个cookie,而在http://localhost:8090/APPTest/目录下的文件就不能够访问该cookie。设置cookie时有一个path属性能够改变cookie的有效访问路径。但是目前path只能设置一个参数即"/",代表是根路径。示例代码如下:
Document.cookie= "Key=escape (value);p ath=/";
如果设置了path= "/" ,则不管设置cookie在哪个路径,在http: //localhost:8090/APPTest/ 下及所有目录及子目录下都能够访问到这个cookie.理论上如果设置path= "\aaa" ,该cookie的作用域应该是aaa目录下及aaa目录下的所有子目录下都能够访问到这个cookie,但实际上并没有实现这样的功能,我认为这也是js实现cookie的一个bug。这里有两个问题需要注意,一是如果设置两个同名的cookie,如http: //localhost:8090/APPTest/aaa/下设置了两个cookie,一个设置了path为"/",另一个cookie不带path参数,那么在http://localhost:8090/APPTest/aaa/会访问到两个同名的cookie值,而在路径为http://localhost:8090/APPTest/只能访问到全局的cookie值。但是我们没有办法通过路径去区分。第二个需要注意的就是删除cookie,如果设置cookie时带path属性,那么在删除的时候一定要加上path属性,否则删除的是当前目录下设置的cookie值。In addition to setting the cookie can also set two properties, respectively, domain and secure,domain to set the cookie Access field, below I give the domain basic theory. 例如:http: //www.google.com/和gmail.google.com就是两个不同的主机名。默认情况下,一个主机中创建的cookie在另一个主机下是不能被访问的,但可以通过domain参数来实现对其的控制,其语法格式为:

 

1 document.cookie= "Name=value;domain=cookiedomain";

Take Google, for example, to achieve cross-host access, can be written as:

Document.cookie= "name=value;domain=.google.com";
这样,所有google.com下的主机都可以访问该cookie。 因为这个参数我没有测试过也没有用过,所以如果当用到这个参数,可以参考上面的理论部分。 secure代表该cookie是否是安全的。如果设置了该属性,只有使用https协议才能够访问到 该cookie. 下面给出cookie的完整格式
1 name=[, expires=][, domain=][, path=[; secure]2 name =< value >[; expires=< date >][; domain=< domain >][; path=< path >][; security]

Cookies in JS use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.