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 to understand cookie in JavaScript

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

Share

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

This article is about how to understand cookie in JavaScript. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Good programmer web front-end training sharing JavaScript learning notes cookie,cookie is a location to store data in the form of strings

Each HTTP request carries the cookie to the server in the request header

Each HTTP response carries cookie to the client in the response header

In other words, cookie is the data that automatically moves between the client and the server without our manual setting.

We just need to set the content of cookie.

Storage form of COOKIE

Cookie is stored as a string and appears as key=value in a string

Each key=value is a piece of data

To divide between multiple data.

/ / the form of cookie: 'aplomb 100; bread200; cymbidium 300'

Characteristics of COOKIE

00001. Storage size is limited, usually about 4 KB

00002. There is a limit on the quantity, usually about 50

00003. Timeliness, that is, there is an expiration time, usually at the session level (that is, the browser expires when the browser is closed)

00004. There is a domain name restriction, that is, who sets it and who can read it.

Mode of use

Use [xss_clean] to read the contents of cookie

Const cookie = [xss_clean] console.log (cookie) / / can get the value of the current cookie

Set the content of cookie to use [xss_clean]

/ / set a cookie with timeliness to session level [XSS _ clean] ='a cookie with expiration time [XSS _ clean] = 'cookie with expiration time, 18 Dec 2043 12:00:00 GMT ";' / / the above cookie data will expire after 12:00 on December 18, 2043, and will disappear automatically after expiration

Delete the contents of cookie using [xss_clean]

/ / because cookie cannot delete directly / / so we can only set the expiration time of a cookie to before the current time / / then the browser will automatically delete cookie [XSS _ clean] = 'baked 200 / cookie cookie Thu, 18 Dec 2018 12:00:00 cookie ";'

COOKIE operation encapsulation

Because there is no special method to operate the addition, deletion, modification and search of COOKIE in js

So we need to encapsulate a method ourselves.

Set up cookie

/ * setCookie is used to set cookie * @ param {STRING} key cookie name to set * @ param {STRING} value cookie content to be set * @ param {NUMBER} expires expiration time * / function setCookie (key, value, expires) {

Const time = new Date ()

Time.setTime (time.getTime ()-1000 * 60 * 60 * 24 * 8 + expires) / / is used to set the expiration time

[xss_clean] = `${key} = ${value}; expires=$ {time};`}

Read cookie

/ * getCookie gets an attribute in cookie * @ param {STRING} key the cookie attribute you want to query * @ return {STRING} the value of the cookie attribute you want to query * / function getCookie (key) {

Const cookieArr = [xss_clean] .split (';')

Let value =''

CookieArr.forEach (item = > {

If (item.split ('=') [0] = key) {

Value = item.split ('=') [1]

}

})

Return value}

Delete cookie

/ * delCookie deletes an attribute in cookie * @ param {STRING} name the name of a cookie attribute you want to delete * / function delCookie (name) {

SetCookie (name, 1,-1)}

The above is how to understand the cookie in JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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