Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can js give the stored cookie value a period of validity and let it expire automatically the next day?

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report