How can js give the stored cookie value a period of validity and let it expire automatically the next day?
This article mainly explains "js how to give the stored cookie value a period of validity and let the next day automatically expire", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how js can give the stored cookie value a period of validity and let it expire automatically the next day.
How can js give the stored cookie value a validity period, let it expire automatically the next day, and count it again? the code is as follows:
Cookie = {
/ * *
* obtain cookie
, /
Get: function (name) {
Var cookieName = encodeURIComponent (name) +'='
CookieStart = [xss_clean] .indexOf (cookieName)
CookieValue = null
If (~ cookieStart) {
Var cookieEnd = [xss_clean] .indexOf (';', cookieStart)
If (! ~ cookieEnd) {
CookieEnd = [xss_clean] .length
}
CookieValue = decodeURIComponent ([xss_clean] .substring (cookieStart + cookieName.length, cookieEnd))
}
Return cookieValue
}
/ * *
* set cookie
, /
Set: function (name, value, expires, path, domain, secure) {
Var cookie = encodeURIComponent (name) +'='+ encodeURIComponent (value)
Expires instanceof Date & (cookie + ='; expires=' + expires.toGMTString ())
Path & (cookie + ='; path=' + path)
Domain & (cookie + ='; domain=' + domain)
Secure & (cookie + ='; secure')
[xss_clean] = cookie
}
/ * *
* delete cookie
* in javascript, there is no api for deleting cookie directly, only by setting the expiration time
* set it to a certain time in the past and let the cookie expire to delete it.
, /
Del: function (name, path, domain, secure) {
This.set (name,'', new Date (0), path, domain, secure)
}
}
The following is to store the cookie and set the expiration date:
/ / cookie expiration time, early the next morning
Var expires = new Date (). GetTime () + 30 * 24 * 60 * 60 * 1000) .setHours (0,0,0,0))
/ / cookie value to be stored
Var value='100'
Cookie.set ("key", value, expires)
At this point, I believe you have a deeper understanding of "how js can give the stored cookie value a period of validity and let it expire automatically the next day". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!