JavaScript settings and ways to get cookies detailed _javascript tips

Source: Internet
Author: User
Tags set cookie setcookie

The examples in this article describe JavaScript settings and how to get cookies. Share to everyone for your reference, specific as follows:

1. Set Cookies

function Setcookie (Cookiename,cookievalue,cookieexpires,cookiepath)
{
  cookievalue = Escape (Cookievalue); /encoded latin-1
  if (cookieexpires== "")
  {
    var nowdate = new Date ();
    Nowdate.setmonth (Nowdate.getmonth () +6);
    Cookieexpires = Nowdate.togmtstring ();
  }
  if (cookiepath!= "")
  {
    cookiepath = "; Path= "+cookiepath;
  }
  document.cookie= cookiename+ "=" +cookievalue+ "; expires=" +cookieexpires+cookiepath;
}

2. Get cookies

function Getcookievalue (cookiename)
{
  var cookievalue = document.cookie;
  var cookiestartat = Cookievalue.indexof ("" +cookiename+ "=");
  if (cookiestartat==-1)
  {
    Cookiestartat = Cookievalue.indexof (cookiename+ "=");
  }
  if (cookiestartat==-1)
  {
    cookievalue = null;
  }
  else
  {
    Cookiestartat = cookievalue.indexof ("=", Cookiestartat) +1;
    Cookieendat = Cookievalue.indexof (";", Cookiestartat);
    if (cookieendat==-1)
    {
      cookieendat = cookievalue.length;
    }
    Cookievalue = unescape (cookievalue.substring (Cookiestartat,cookieendat));//decode Latin-1
  } return
  Cookievalue;
}

Example:

<!doctype html>  

Attention:

Because Google Chrome browser for security only support Online-cookie, so in the local test is not effective, you need to upload to the server to try.

More about JavaScript operation cookie related content to view the site topics: "JavaScript operation Cookie Related knowledge summary" and "jquery Cookie Operation Tips Summary"

I hope this article will help you with JavaScript programming.

Related Article

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.