Add, value, delete instance code of the cookie in JS

Source: Internet
Author: User
Tags setcookie delete cache
The application of cookies in the Web page is very common, in this article for you to introduce how to add to the cookie, value, delete, interested friends do not miss

The code is as follows:

function Setcookie (name,value,time) {var odate = new Date (); Odate.setdate (Odate.getdate () +time); document.cookie = Name + "=" +value+ "; expires=" +odate; } function GetCookie (name) {var arr = document.cookie.split (";"); for (var i=0; i<arr.length; i++) {var arr2 = Arr[i].s Plit ("="); if (arr2[0] = = name) {return arr2[1];}} Return "";} function Removecookie (name) {Setcookie (name, "", 0)}

JavaScript is a script that runs on the client, so it is generally not possible to set the session because the session is running on the server side. The cookie is run on the client, so you can use JS to set the cookie. Now let's analyze the case.

Verified//JavaScript document//Use Description://Set cache: Setcookie ("name", value);//Get cache: var Name=getcookie ("name");// Delete cache: Delcookie ("name");///Set Cookiefunction Setcookie (Nameofcookie, value, expiredays) {//@ parameter: Three variables to set a new cookie:// The name of the cookie, the value of the cookie stored,//and the time the cookie expires. These lines are to convert the number of days to a valid date var expiredate = new Date (); Expiredate.settime (Expiredate.gettime () + (Expiredays * 24 * 3600 * 1000)); The following line is used to store the cookie, simply assign the "Document.cookie" to the value. Note that the date is converted to GMT by using the toGMTString () function. Document.cookie = Nameofcookie + "=" + Escape (value) + ((expiredays = = null)? "" : "; Expires= "+ expiredate.togmtstring ());} Get cookie Value function GetCookie (Nameofcookie) {///First we check if the cookie exists.//If not present, the length of the Document.cookie is 0 if ( Document.cookie.length > 0) {//Then we check if the name of the cookie exists in Document.cookie//Because more than one cookie value is stored, So even if the length of the Document.cookie is not 0, there is no guarantee that we want the name of the cookie to exist//So we need this step to see if there is a cookie we want//if the begin variable is worth 1 then it means that there is no begin =  Document.cookie.indexOf (nameofcookie+ "=");   if (begin! =-1) {//indicates the existence of our cookie. Begin + =The initial position of the Nameofcookie.length+1;//cookie value end = Document.cookie.indexOf (";", begin);//End Position if (end = =-1) end = Document.co  okie.length;//No; End is the string ending position return unescape (document.cookie.substring (begin, end)); }} return null; Cookie does not exist return null}///delete cookiefunction Delcookie (Nameofcookie) {///The function checks if the cookie is set, if set, then the expiration time is adjusted to the past; The rest is left to the operating system at the appropriate time to clean up the cookie if (GetCookie (Nameofcookie)) {Document.cookie = Nameofcookie + "=" + "; Expires=thu, 01-jan-70 00:00:01 GMT "; }}

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.