Simple Method for reading and writing cookies using js (recommended), recommended for reading and writing cookies using js

Source: Internet
Author: User

Simple Method for reading and writing cookies using js (recommended), recommended for reading and writing cookies using js

When the data security requirements are not very high, we can directly use js to read and write cookies, which is more convenient.

The Code is as follows:

/* Function: Save the cookie function parameters: name, cookie name; value, value */function SetCookie (name, value) {var Days = 30*12; // cookie will be saved for one year var exp = new Date (); // get the current time exp. setTime (exp. getTime () + Days * 24*60*60*1000); // change to document in milliseconds. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();}/* function: Obtain the cookie function parameter: name, cookie name */function getCookie (name) {var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $)"); If (arr! = Null) {return unescape (arr [2]);} else {return null ;}/ * function: delete cookie function parameter: name, cookie name */function delCookie (name) {var exp = new Date (); // The current time exp. setTime (exp. getTime ()-1); var cval = getCookie (name); if (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();}

The simple method (recommended) for reading and writing cookies using js is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the customer's house.

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.