//Verified
// JavaScript Document
//Instructions for use:
//Set cache: setCookie("name",value);
//Get cache: var name=getCookie("name");
//Delete cache :delCookie("name");
///Set cookie
function setCookie(NameOfCookie, value, expiredays)
{
//@Parameters: Three variables are used to set new cookies:
//The name of the cookie, the stored cookie value,
// and the cookie expiration time.
// These lines convert the number of days into a legal date
var ExpireDate = new Date ( );
ExpireDate.setTime(ExpireDate.getTime() (expiredays * 24 * 3600 * 1000));
// The following line is used to store cookies, just simply "document.cookie" Just assign a value.
// Note that the date is converted into GMT time through the toGMTstring() function.
document.cookie = NameOfCookie "=" escape(value) ((expiredays == null) ? "" : "; expires=" ExpireDate.toGMTString());
}
///Get cookie Value
function getCookie(NameOfCookie)
{
// First we check whether the cookie exists.
// If it does not exist, the length of document.cookie is 0
if (document.cookie .length > 0)
{
// Then we check whether the cookie name exists in document.cookie
// Because more than one cookie value is stored, even if the length of document.cookie is not 0 There is no guarantee that the cookie with the name we want exists
//So we need this step to see if there is a cookie we want
//If the variable value of begin is -1, it means it does not exist
begin = document.cookie.indexOf(NameOfCookie "=");
if (begin != -1)
{
// Indicates the existence of our cookie.
begin = NameOfCookie.length 1;//Initial position of cookie value
end = document.cookie.indexOf(";", begin);//End position
if (end == -1) end = document.cookie.length; //No; then end is the end position of the string
return unescape(document.cookie.substring(begin, end));
}
}
return null;
// cookie is not Returns null
}
///Delete cookie
function delCookie (NameOfCookie)
{
// This function checks whether the cookie is set, and if it is set, the expiration time is moved to the past time;
//The rest is left to the operating system to clear the cookie at the appropriate time
if (getCookie(NameOfCookie))
{
document.cookie = NameOfCookie "=" "; expires=Thu , 01-Jan-70 00:00:01 GMT";
}
}
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn