JS Operation Cookie Method

Source: Internet
Author: User
Tags set cookie

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

functionGet_cookie (Name) {varSearch = Name + "="//query retrieves the valuevarreturnvalue = "";//return valueif(Document.cookie.length > 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))}} 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:

// Get current Time var date=New  date (); var expiresdays=10; // set date to 10 days after the time Date.settime (Date.gettime () +expiresdays*24*3600*1000); // set the 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:

// Get current Time var date=New  date (); // sets date to the past time Date.settime (Date.gettime () -10000); // Delete the UserID cookie

The above method is encapsulated below

varCookie ={set:function(Key,val,time) {//Set Cookie Method        varDate=NewDate ();//Get current Time        varExpiresdays=time;//set Date to n days afterDate.settime (Date.gettime () +expiresdays*24*3600*1000);//time to format cookie recognitionDocument.cookie=key + "=" + val + "; expires=" +date.togmtstring ();//Set Cookies}, get:function(key) {//Get Cookie Method        /*Get Cookie Parameters*/        varGetCookie = Document.cookie.replace (/[]/g, "");//gets the cookie and formats the obtained cookie, removing the space character        varArrcookie = Getcookie.split (";")//The obtained cookie is identified with a "semicolon" to save the cookie to an array of Arrcookie        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            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 statementTIPS=ARR[1];//assigning the value of a cookie to a variable tips                 Break;//terminating A 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 Cookies
} returntips; }}

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.