JS Operation Cookie Method

Source: Internet
Author: User
Tags set cookie

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

function Get_cookie (name) {   var search = name + "="//query retrieves the value   var returnvalue = "";//return value   if (document.cookie.le Ngth > 0) {     sd = document.cookie.indexOf (search);     if (sd!=-1) {        SD + = search.length;        End = Document.cookie.indexOf (";", SD);        if (end = =-1)         end = Document.cookie.length;
The unescape () function decodes a string encoded by escape (). Returnvalue=unescape (document.cookie.substring (SD, end)) } } return returnvalue;}
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:

Gets the current time var date=new date (); var expiresdays=10;//set date to 10 days after Date.settime (Date.gettime () +expiresdays*24*3600* 1000);//Set userId and username two cookies to expire after 10 days 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:

The above method is encapsulated below

var cookie = {Set:function (key,val,time) {//Set Cookie method var date=new date ();//Get current time Var expiresdays=time  ; Set Date to n days after the time Date.settime (Date.gettime () +expiresdays*24*3600*1000);  Time Document.cookie=key + "=" + val + "expires=" +date.togmtstring () formatted as cookie recognition;  Set Cookie}, Get:function (key) {//Get Cookie Method */* Get cookie parameter */var GetCookie = Document.cookie.replace (/[  ]/g, "");  Gets the cookie and formats the obtained cookie, removing the space character var Arrcookie = Getcookie.split (";")  The obtained cookie is identified by a "semicolon" as a "comma" to save the cookie to an array of Arrcookie Var tips; Declare variable tips for (var i=0;i<arrcookie.length;i++) {//use for loop to find tips variable var arr=arrcookie[i].spli in cookie   T ("=");                A single cookie is identified with an "equal sign", and a single cookie is saved as an ARR array if (Key==arr[0]) {//Match variable name, where arr[0] is the cookie name, and if the bar variable is a tips, the assignment operation in the judgment statement is performed   TIPS=ARR[1];   Assign the value of the cookie to the variable tips break; Terminate for Loop Traversal}},
Delete:function (key) {//Delete cookie method
var date = new Date (); Get current time
Date.settime (Date.gettime ()-10000); Set date to the past time
Document.cookie = key + "=V; expires = "+date.togmtstring ();//Set Cookie
} return tips; }}

How to use:

Cookie.set ("UESR", "SSS", 24);//set to 24-day expiration
Alert (Cookie.get ("UESR"));//Get Cookie

JS Operation Cookie Method

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.