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 operate Cookie in JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to operate Cookie in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to operate Cookie in JavaScript.

Cookie is just a small text file stored in a computer browser. They contain the following data:

Name-value pairs that save data

The cookie expires when the date expires

The domain and path of the server to which it should be sent

Cookie also has some limitations that are worth mentioning:

The maximum of each cookie is 4096 bytes

Up to 20 cookie per domain (slightly different per browser)

Cookie is dedicated to its own domain name (the website cannot read the Cookie of other domains, it can only be its own)

The size limit applies to the entire cookie, not just its value

In the browser, cookie is exposed as [xss_clean] s through DOM.

Next we will show you how to use JavaScript to set up, get, update, and delete Cookie data in the browser.

Create Cookies

Setting up cookie in a browser with JavaScript is very easy! I'll show you below.

(1) set Cookie

The following is the execution code to create a new cookie with JavaScript in the browser:

[xss_clean] = "userId=nick123"

After running the code, open the browser and you should be able to find the cookie in the developer tool's Application (Safari or Chrome) or Storage (Firefox).

(2) set the expiration time of cookie

You can also add an expiration time (UTC) to the Cookie to tell the browser when it should be deleted:

[xss_clean] = "userId=nick123; expires=Wed, 15 Jan 2020 12:00:00 UTC"

(3) set a Cookie path

You can also tell the browser the path to which cookie belongs (the default is the path of the current page):

[xss_clean] = "userId=nick123; expires=Wed, 15 Jan 2020 12:00:00 UTC; path=/user"

(4) set cookie domain

The last piece of data we will introduce is the domain to which cookie belongs (the default is the current domain):

[xss_clean] = "userId=nick123; expires=Wed, 15 Jan 2020 12:00:00 UTC; path=/user; domain=mysite.com"

Read Cookies

Through the [xss_clean] object, it is also very easy to read cookie with JavaScript:

(1) read all the Cookie of a single page

Get all the cookie of a single page as a string, with each cookie separated by a semicolon:

Const cookies = [xss_clean]

(2) read Cookie with a specific name

To access a cookie with a specific name, we need to get all the cookie on the page, parse the string, and then look for a match of the cookie name we are looking for.

This is a function that does this with regular expressions:

Function getCookieValue (name) {let result = [xss_clean] .match ("(^ | [^;] +)\\ s *" + name + "\\ s * ([^;] +)") return result? Result.pop (): ""}

You use this function like this:

GetCookieValue ("userId") / / returns nick123

This returns the string value corresponding to the name parameter supplied to the function.

If you haven't mastered regular expressions yet, there's another function with the same function:

Function getCookieValue (name) {const namenameString = name + "=" const value = [xss_clean] .split (";") .filter (item = > {return item.includes (nameString)}) if (value.length) {return value [0] .substring (nameString.length, value [0] .length)} else {return ""}}

Use this function in the same way:

GetCookieValue ("userId") / / returns nick123

Update Cookies

You can change the value of cookie by overriding it with the new value by creating it.

You can use this code to override the cookie "userId" created earlier in this article:

[xss_clean] = "userId=new_value"

When you run the getCookieValue function again, the new value will be returned:

GetCookieValue ("userId") / / returns new_value

Delete Cookies

You can delete cookie by setting a null value and setting its expiration date to any time in the past.

If we want to delete the cookie "userId" from the previous example, follow these steps:

[xss_clean] = "userId=; expires=Thu, 01 Jan 1970 00:00:00 UTC;". Now that you have a better understanding of "how to operate Cookie in JavaScript", 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

Internet Technology

Wechat

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

12
Report