JS Operation Cookie

Source: Internet
Author: User

A cookie is a variable stored on a visitor's computer. This cookie is sent whenever a page is requested by the same computer through a browser. You can use JavaScript to create and retrieve the value of a cookie.

Add a cookie

Describe:

Create a new cookie to be managed by the browser!

Parameter description:

Name-Key of the key-value pair, which uniquely marks a value

Value-values of key-value pairs, contents of the cookie Store

Expdays-cookie Expiration Time (active time)

function Setcookie (name, value, expdays) {    var expdate = new Date ();//Set cookie expiration date    expdate.setdate ( Expdate.getdate () + expdays); Add a cookie    document.cookie = name + "=" + Escape (value) + "; expires=" + expdate.toutcstring ();}

Get cookies

Describe:

Based on the name of the parameter, get the value of the corresponding value in the cookie

function GetCookie (name) {//Gets the name in the cookie starting and ending position    var start = document.cookie.indexOf (name + "=");    if (Start! =-1) {        start = start + name.length + 1;//Gets the terminating position of value        var end = Document.cookie.indexOf (";", start); C6/>if (end = =-1) end = Document.cookie.length; Intercepts the value of the cookie and returns the return        unescape (document.cookie.substring (Start, end));    }    Return "";}

  

Delete Cookies

Describe:

Delete a cookie based on name (set to expire immediately)

function Delcookie (name) {    Setcookie (name, "",-1);}

  

Extended Features:

Pre-agreed, same key, multiple value,value separated by commas (,)

Determine if a value already exists

function Isexistvalue (key, Val) {  var value = GetCookie (key);  var index = Value.indexof (val);  return Index! =-1;}

  

  

JS Operation Cookie

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.