Cookie operation in JS

Source: Internet
Author: User

Several common methods of manipulating cookies in JS

* The concept of domain exists in cookies, using path and domain to differentiate

Get cookies

functionGetCookie (c_name) {if(document.cookie.length>0) {console.log (document.cookie); C_start=document.cookie.indexof (c_name + "="); if(c_start!=-1) {c_start=c_start + c_name.length+1; C_end=document.cookie.indexof (";", c_start); if(c_end==-1) c_end=document.cookie.length; returnunescape (document.cookie.substring (c_start,c_end)); }     }    return"";}

Set cookies

function setcookie (c_name,value,expiredays) {    var cookiestr = "";     var exdate=New  Date ();    Exdate.setdate (exdate.getdate ()+expiredays);     = c_name+ "=" +escape (value) +    ((expiredays= =null)? "" : "; expires= "+exdate.togmtstring ()) +";p ath=/";} // Because of the concept of the domain of the cookie, and here to not distinguish the domain, to obtain the value of the cookie, so here is the unified path path=/;

Delete Cookies

1.

function Delete_cookie (name, path, domain) {  if(get_cookie (name)) {    = name + "=" +
    ? ";p ath=" +path: ") +      (domain)?"; domain= "+domain:" ") +      "; expires=thu, 1970 00:00:01 GMT ";}  }

2.

function Delete_cookie (name) {  = name + ' =; expires=thu, 1970 00:00:01 GMT; ;}

3.

function Delcookie (c_name) {     varnew  Date ();      -1);      var cval=GetCookie (c_name);     if (cval!=null)         document.cookie= c_name + "=;expires=" +

Reference: http://www.w3schools.com/js/js_cookies.asp

Cookie operation in JS

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.