Use JS to set cookies, read cookies, delete cookies

Source: Internet
Author: User
Tags setcookie

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.

Suppose there is a case, in a use case process, a page jumps to page B, if the a page with the variable temp Save the value of a variable, in the B page, also need to use JS to reference the variable value of temp, for JS in the global variable or static variable life cycle is limited, When a page jump occurs or the page closes, the values of these variables are re-loaded, that is, the saved effect is not achieved. The best solution to this problem is to use a cookie to hold the value of the variable, so how do you set and read the cookie?

First, we need to understand a little bit about the structure of the cookie, simply: The cookie is stored in the form of a key-value pair, the Key=value format. Each cookie is generally ";" Separated.

JS Settings Cookie:

Suppose you want to save the value of the variable username ("Jack") in the a page to the cookie, the key value is name, then the corresponding JS code is:

Document.cookie= "Name=" +username;  



JS Read Cookie:

Assume that the content stored in the cookie is: name=jack;password=123

The JS code that gets the value of the variable username in the B page is as follows:

var username=document.cookie.split (";") [0].split ("=") [1];  



//js how to operate cookies!

//Write CookiesfunctionSetcookie (name,value) {varDays = 30; varExp =NewDate (); Exp.settime (Exp.gettime ()+ days*24*60*60*1000); Document.cookie= name + "=" + Escape (value) + "; expires=" +exp.togmtstring ();} //Read CookiesfunctionGetCookie (name) {vararr,reg=NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)"); if(arr=Document.cookie.match (reg))returnUnescape (arr[2]); Else         return NULL; } //Delete CookiesfunctionDelcookie (name) {varExp =NewDate (); Exp.settime (Exp.gettime ()-1); varCval=GetCookie (name); if(cval!=NULL) Document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();} 


Using the example

Setcookie ("name", "Hayden"); alert (GetCookie (



If you need to set a custom expiration time
Then replace the above Setcookie function with the following two functions OK;


//Program CodefunctionSetcookie (name,value,time) {varStrsec =getsec (time); varExp =NewDate (); Exp.settime (Exp.gettime ()+ strsec*1); Document.cookie= name + "=" + Escape (value) + "; expires=" +exp.togmtstring ();} functiongetsec (str) {alert (str); varStr1=str.substring (1,str.length) * *; varStr2=str.substring (0,1); if(str2== "S")   {         returnstr1*1000; }   Else if(str2== "H")   {        returnstr1*60*60*1000; }   Else if(str2== "D")   {        returnstr1*24*60*60*1000; } } 


This is an example of using a set expiration time:
S20 is represented for 20 seconds
H refers to the hour, such as 12 hours: H12
D is the number of days, 30 days: D30

Setcookie ("name", "Hayden", "S20");

Use JS to set cookies, read cookies, delete cookies

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.