JS sets cookie reads cookie and deletes cookie

  • 2020-05-30 19:23:23
  • OfStack

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

cookie runs on the client side, so you can use JS to set cookie.

Assuming there is one kind of situation, in some cases in the process, by A page jump to B page, if the variable is used in A pages using JS temp saved a 1 the value of a variable, when B page, you need to use the same JS to reference temp variable values, for the global or static variables in JS lifecycle is limited, when a page jump or shut down, the values of these variables will reload, which didn't reach the effect of preservation. The best solution to this problem is to use cookie to save the value of the variable, so how do you set and read cookie?

First of all, we need to know a little about the structure of cookie. To put it simply, cookie is saved in the form of key-value pairs, that is, key=value. Between each cookie, 1 is generally followed by "; ". Space.

JS set cookie:

If the value of the variable username ("jack") is saved in A page to cookie, and key is name, the corresponding JS code is:


document.cookie="name="+username;

Read JS cookie:

Suppose that the contents stored in cookie are: name=jack; password = 123

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


var username=document.cookie.split(";")[0].split("=")[1];
//JS operation cookies methods !
// write cookies
function setCookie(name,value)
{
var Days = 30;
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 sample 
setCookie("name","hayden");
alert(getCookie("name"));
// If you need to set a custom expiration time 
// So let's take the top one setCookie I'm going to replace it with the next two functions ok;
// The 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 a usage example with a set expiration time: 
//s20 To represent 20 seconds 
//h It means hours, such as 12 Hour is: h12
//d Is the number of days, 30 Days are: d30
setCookie("name","hayden","s20");

That's all for this article, I hope you enjoy it.


Related articles: