JS set cookies, read cookies, delete Cookie_javascript tips

Source: Internet
Author: User
Tags setcookie

JavaScript is a script that runs on the client, so it is generally not possible to set a session because the session is run on the server side.

Cookies are run on the client, so you can use JS to set cookies.

Suppose there is a case in which, in a use-case process, from a page to the B page, if the a page using JS variable temp saved a variable of the value, in the B page, the same need to use JS to reference the TEMP variable value, for JS global variable or static variable life cycle is limited, When a page jump occurs or the page closes, the values of these variables are loaded back in, that is, the saved effect is not reached. The best solution to this problem is to use cookies to save the value of the variable, so how do you set and read cookies?

First you need to know a little bit about the structure of the cookie, simply put: The cookie is stored as a key-value pair, i.e., the key=value format. Each cookie is generally ";" Separated.

JS Set Cookies:

Assuming that the value of the variable username ("Jack") in the page A is stored in the cookie, the key value is name, the corresponding JS code is:

Copy Code code as follows:

Document.cookie= "Name=" +username;

JS Read cookies:

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

The JS code to get the value of the variable username in page B is as follows:

var username=document.cookie.split (";") [0].split ("=") [1];
JS Operation Cookie Method!
Write Cookies
function Setcookie (name,value)
{
var days =;
var exp = new Date ();
Exp.settime (Exp.gettime () + days*24*60*60*1000);
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}

Read cookies

function GetCookie (name)
{
var arr,reg=new RegExp ("(^|)" +name+ "= ([^;] *)(;|$)");
if (Arr=document.cookie.match (reg)) return
unescape (arr[2]);
else return
null;
}

Delete Cookies

function Delcookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null)
document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();
}
Use the example
Setcookie ("name", "Hayden");
Alert (GetCookie ("name"));
If you need to set custom expiration time
//Then change the above Setcookie function to the following two functions OK;
Program code
function Setcookie (name,value,time)
{
var strsec = getsec (time);
var exp = new Date ();
Exp.settime (Exp.gettime () + strsec*1);
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}
function Getsec (str)
{
alert (str);
var str1=str.substring (1,str.length) *1;
var str2=str.substring (0,1);
if (str2== "s")
{return
str1*1000;
}
else if (str2== "H")
{return
str1*60*60*1000;
}
else if (str2== "D")
{return
str1*24*60*60*1000
}
} This is an example of a set expiration time:
//S20 is for 20 seconds
//h refers to hours, such as 12 hours is: H12
//d is the number of days, 30 days: D30 Setcookie
("name", "Hayden "," S20 ");

The above mentioned is the entire content of this article, I hope you can enjoy.

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.