The
javascript column introduces the different uses of cookies.

Using JS to operate Cookies is actually very troublesome. There is no simple API that allows us to obtain or set Cookies.
The only API that operates Cookie is document.cookie, but this code is very uncomfortable to use. If we want to get a required Cookie, we may have to write such a utils function first:
function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift();
}复制代码
But in Chrome 87 version we no longer need to introduce such code, instead it is a new API: cookieStore. This is an asynchronous API that can easily obtain settings and monitor cookie changes.
If you want to download the beta version of Chrome, you can get it at this link .
The following is an introduction to the new content.
Getting Cookie
Just now we have learned how troublesome it was to get a required Cookie before. Now we can get the content we want in just one sentence.

cookieStore.get There are two function signatures. In the former, we can match the desired content by passing in the cookie attributes, and in the latter, Directly pass in name to obtain. The API is very intuitive and is infinitely better than the previous approach.
Of course, in addition to obtaining a single cookie, the new API also provides a way to obtain multiple cookies. For example, now if I want to get all the cookies belonging to a certain domain, I can use the following method:

Set Cookie
If we need to set Cookie before If so, you should write code similar to the following, still operating document.cookie
const setCookie = (name, value, days = 7, path = '/') => { const expires = new Date(Date.now() + days * 864e5).toUTCString() document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + path
}复制代码
Now we can easily set cookies through cookieStore.set:

set function supports two signatures, the former can set the content on all cookies, and the latter is key-value form.
Delete Cookie
After talking about obtaining and deleting Cookie, the corresponding deletion operation is definitely indispensable.
Before this, if you wanted to delete a cookie, you needed to set the expiration time of the cookie in the past. Once it expires, it will automatically become invalid.
var delete_cookie = function(name) { document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};复制代码
It seems strange. If you want to delete a cookie, you don’t delete the field, but expire it. Now that we have a new API, we don’t need to do this:

Similarly, the delete API also has two function signatures, a simple string It is equivalent to the name that needs to be deleted. When passing an object, the signature is slightly different from before. It should be noted that the optional attributes in the signature have default values:
dictionary CookieStoreDeleteOptions {
required USVString name;
USVString? domain = null;
USVString path = "/";
};复制代码
Monitoring Cookie Change
This function should not have existed before. Now you can monitor the changes and deletions of Cookies through the new API.

When we set or delete Cookie, the corresponding event will throw out the content we changed.
Finally
The above is the content of this article. The link is the document of cookieStore. Interested readers can learn more.
The theme of the Front-end Frontier Observer series focuses on new APIs, specifications, technologies, etc. in the front-end direction. Although we may not be able to enjoy the benefits of these APIs in the short term, one day or polyfill will allow us Use these things.
Related free learning recommendations: javascript(Video)
The above is the detailed content of Cookie can actually be adjusted like this. For more information, please follow other related articles on the PHP Chinese website!
Python vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AMPython and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
From C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AMThe shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
JavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AMDifferent JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
Beyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AMJavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AMI built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AMThis article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base
JavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AMJavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.
The Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AMThe latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function







