Cookie is a useful function that can determine a certain State. We will share with you how to add a Cookie in JS to retrieve the cookie value and delete the cookie, if you are interested, you can refer to a very useful cookie function to determine a certain status, which has a high usage rate in the project.
The Code is as follows:
Function setCookie (name, value)
{
Var Days = 30;
Var exp = new Date ();
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
Var strsec = getsec (time );
Var exp = new Date ();
Exp. setTime (exp. getTime () + strsec * 1 );
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 ();
}