Cookies
Cookies, sometimes in their plural form, are the data (usually encrypted) stored on the user's local terminal by certain websites in order to identify the user and track the session. Definitions of RFC2109 and 2965 have been deprecated, and the newly superseded specification is RFC6265.
The role of Cookies
The server can use cookies to contain information that is arbitrary to filter and regularly maintain this information in order to determine the status in the HTTP transmission. The most typical application of cookies is to determine whether a registered user has logged on to the site, and users may be prompted whether to retain user information for the next time they enter the site in order to simplify the login process, which is the function of cookies. Another important application is the "shopping cart" process. Users may select different items on different pages of the same site for a period of time, which will be written to cookies to extract information at the end of the payment.
JS Settings Cookie
Document.cookie= "Popped=yes"
JS Get Cookie
1 functionGet_cookie (Name) {2 varSearch = Name + "="//value retrieved by query3 varreturnvalue = "";//return value4 if(Document.cookie.length > 0) {5SD =document.cookie.indexOf (search);6 if(Sd!=-1) {7SD + =search.length;8End = Document.cookie.indexOf (";", SD);9 if(end = =-1)TenEnd =document.cookie.length; One //The unescape () function decodes a string encoded by escape (). AReturnvalue=unescape (document.cookie.substring (SD, end)) - } - } the returnreturnvalue; - } - //How to use: -Get_cookie ("popped");
Set end date for cookies
For example: If you want to set the cookie to expire after 10 days, you can do so:
1 // Get current Time 2 var date=New date (); 3 var expiresdays=10; 4 // set Date to 10 days later 5 Date.settime (Date.gettime () +expiresdays*24*3600*1000); 6 // set the UserID and username two cookies to expire after 10 days 7 document.cookie= "userid=828; Username=hulk; Expires= "+date.togmtstring ();
Where gmt_string is the time string in GMT format, this statement is to set the UserID this cookie to gmt_string represents the expiration time, over this time, the cookie will disappear, inaccessible.
Delete Cookies
In order to delete a cookie, you can set its expiration time to a past time, for example:
1 // Get current Time 2 var date=New date (); 3 // set Date to the past time 4 Date.settime (Date.gettime () -10000); 5 // Delete the UserID cookie 6 document.cookie= "userid=828; Expires= "+date.togmtstring ();
In the following package, the above method, JS operation Cookie Operation class Package
(This is a package I found from the Internet, but the original test a bit of a bug, after the modification can be used, and later if a better encapsulation method I will continue to share)
1 varCookie = {2Setfunction(Key,val,time) {//Set Cookie Method3 varDate=NewDate ();//Get current Time4 varExpiresdays=time;//set Date to n days after5Date.settime (Date.gettime () +expiresdays*24*3600*1000);//time to format cookie recognition6Document.cookie=key + "=" + val + "; expires=" +date.togmtstring ();//Set Cookies7 },8Getfunction(key) {//Get Cookie Method9 /*Get Cookie Parameters*/Ten varGetCookie = Document.cookie.replace (/[]/g, "");//gets the cookie and formats the obtained cookie, removing the space character One varArrcookie = Getcookie.split (";")//The obtained cookie is identified with a "semicolon" to save the cookie to an array of Arrcookie A varTips//declaring variable Tips - for(vari=0;i<arrcookie.length;i++) {//use a For loop to find tips variables in cookies - varArr=arrcookie[i].split ("=");//identify a single cookie with an "equal sign" and save a single cookie as an arr array the if(Key==arr[0]) {//match the variable name, where arr[0] refers to the cookie name, and if the bar variable is a tips, perform the assignment in the Judgment statement -TIPS=ARR[1];//assigning the value of a cookie to a variable tips - Break;//terminating A For loop traversal - } + } - returntips; + }, A Delete:function(key) {//Delete Cookie Method at varDate =NewDate ();//Get current Time -Date.settime (Date.gettime ()-10000);//set Date to the past time -Document.cookie = key + "=V; expires = "+date.togmtstring ();//Set Cookies - } -}
How to use:
Cookie.set ("UESR", "SSS", 24);//set to 24-day expiration
Alert (Cookie.get ("UESR"));//Get Cookie
JS Operation Cookie Method