JS Encapsulation Cookie Operation function instance (set, read, delete) _javascript tips

Source: Internet
Author: User
Tags set cookie setcookie

The example of this article describes the JS encapsulation cookie operation function. Share to everyone for your reference, specific as follows:

/* Set cookie*/
function Setcookie (name, value, Iday)
{
  var odate=new Date ();
  Odate.setdate (Odate.getdate () +iday);
  document.cookie=name+ ' = ' +value+ '; expires= ' +odate;
};
/* Usage: Setcookie (' User ', ' Simon ', one); *///
* Get cookie*/
function GetCookie (name)
{
  var arr= Document.cookie.split ('; '); Multiple cookie values are in; Separate, split the cookie and assign it to the array for
  (var i=0;i<arr[i].length;i++)//calendar array
  {
    var arr2=arr[i].split (' = ') ; The original cut of the array is: User=simon, and then split (' = ') divided into: User Simon can be arr2[0] arr2[1] To obtain the user and Simon 
    if (arr2[0]==name)// If the property name of the array is equal to the incoming name
    {return
      arr2[1];//
    returns null
  }
if not found; /* Usage: GetCookie (' user ') *////
* Delete cookie*/
function Removecookie (name)
{
  Setcookie (name, 1,-1); -1 is to tell the system has expired, the system will immediately delete the cookie
};
/* Use method: Removecookie (' user ') * *

I hope this article will help you with JavaScript programming.

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.