Table of Contents
Cookies, sometimes also used in their plural form Cookies, refer to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking. In this article, we will share with you the knowledge of obtaining cookies and deleting cookies with JavaScript, hoping to help you. " >Cookies, sometimes also used in their plural form Cookies, refer to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking. In this article, we will share with you the knowledge of obtaining cookies and deleting cookies with JavaScript, hoping to help you.
Where do cookies exist? " >Where do cookies exist?
What does the cookie look like? " >What does the cookie look like?
If I want to get the cookie based on the key name, how to do it? " >If I want to get the cookie based on the key name, how to do it?
What if I want to delete cookies based on key names? " >What if I want to delete cookies based on key names?
Home Web Front-end JS Tutorial Detailed explanation of JavaScript obtaining cookies and deleting cookies

Detailed explanation of JavaScript obtaining cookies and deleting cookies

Dec 12, 2017 pm 02:29 PM
cookie ie javascript

Cookies, sometimes also used in their plural form Cookies, refer to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking. In this article, we will share with you the knowledge of obtaining cookies and deleting cookies with JavaScript, hoping to help you.

Where do cookies exist?

Exists in document.cookie

Cookie is a string that looks like this:
"name=xxx; age=22;"

## Note: There is a space after the semicolon. Remember this, the following code needs special attention to this point

If I want to get the cookie based on the key name, how do I do it?
<span style="font-size: 16px;">function getCookie(name) {<br>  var value = '; '+ document.cookie;<br>  var parts = value.split('; ' + name + '=');<br>  if(parts.length === 2) {<br>    return parts.pop().split(';').shift();<br>  }<br>}<br></span>
Copy after login
Copy after login

Principle analysis:Assume that the current value of document.cookie is:
myName=xxx; age=22; food=apple; <span style="font-size: 16px;"></span>
var value = '; '+ document.cookie;<span style="font-size: 16px;"></span>make it ; myName=xxx; age=22; food=apple;<span style="font-size: 16px;"></span>
var parts = value.split('; ' + name + '=');<span style="font-size: 16px;"></span> Assume that the incoming name<span style="font-size: 16px;"></span> is age<span style="font-size: 16px;"></span>, then the string will be split according to ; age=<span style="font-size: 16px;"></span>. The array obtained after splitting is:
['myName=xxx', '22; food=apple;']<span style="font-size: 16px;"></span>
if(parts. length === 2)<span style="font-size: 16px;"></span> indicates that the corresponding value is obtained according to the key name, parts.pop()<span style="font-size: 16px;"></span> returns is the last item in the array, that is, 22; food=apple;<span style="font-size: 16px;"></span>, and then call split(';' )<span style="font-size: 16px;"></span>Get the array['22', 'food=apple;']<span style="font-size: 16px;"></span>, and then call shift()<span style="font-size: 16px;"></span> will return the first item of the array, which is 22, and we can get the value we want

Thoughts: var value = '; '+ document.cookie;<span style="font-size: 16px;"></span> This code is the essence of the entire method.

What if I want to delete cookies based on key names?
<span style="font-size: 16px;">function deleteCookie(name) {<br>  document.cookie = name + '=;  expires=Thu, 01 Jan 1970 00:00:01 GMT;'<br>}<br></span>
Copy after login
Copy after login

Principle analysis: Set the cookie expiration time to be less than the current time, then the cookie will be deleted.

#Where do cookies exist?

Exists in document.cookie

What does the cookie look like?

Cookie is a string that looks like this: "name=xxx; age=22;"

## Note: There is a space after the semicolon. Remember this. Pay special attention to this in the following code

<span style="font-size: 16px;">function getCookie(name) {<br>  var value = '; '+ document.cookie;<br>  var parts = value.split('; ' + name + '=');<br>  if(parts.length === 2) {<br>    return parts.pop().split(';').shift();<br>  }<br>}<br></span>
Copy after login
Copy after login

Principle analysis:
Assume that the current value of document.cookie is:
<span style="font-size: 16px;">myName=xxx; age=22; food=apple; </span>
<span style="font-size: 16px;">var value = '; '+ document.cookie;</span>make it <span style="font-size: 16px;">; myName=xxx; age=22; food=apple;</span>
<span style="font-size: 16px;">var parts = value.split('; ' + name + '=');</span> Assume that the incoming <span style="font-size: 16px;">name</span> is <span style="font-size: 16px;"> age</span>, then the string will be split according to <span style="font-size: 16px;">; age=</span>. The array obtained after splitting is:
<span style="font-size: 16px;">['myName=xxx', '22; food=apple;']</span>
<span style="font-size: 16px;"> if(parts. length === 2)</span> indicates that the corresponding value is obtained according to the key name, <span style="font-size: 16px;">parts.pop()</span> returns is the last item in the array, that is, <span style="font-size: 16px;">22; food=apple;</span>, and then call <span style="font-size: 16px;">split(';' )</span>Get the array<span style="font-size: 16px;">['22', 'food=apple;']</span>, and then call <span style="font-size: 16px;">shift()</span> will return the first item of the array, which is 22, and we can get the value we want

Thoughts: <span style="font-size: 16px;">var value = '; '+ document.cookie;</span> This code is the essence of the entire method.

What if I want to delete cookies based on key names?

<span style="font-size: 16px;">function deleteCookie(name) {<br>  document.cookie = name + '=;  expires=Thu, 01 Jan 1970 00:00:01 GMT;'<br>}<br></span>
Copy after login
Copy after login

Principle analysis:
Set the cookie expiration time to be less than the current time, then the cookie will be deleted.

Related recommendations:

How to use cookies with jQuery

Cookie attribute and method analysis

Basic operations for adding and deleting cookies in JavaScript

The above is the detailed content of Detailed explanation of JavaScript obtaining cookies and deleting cookies. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) Feb 10, 2024 am 10:30 AM

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Frequently Asked Questions and Solutions about Cookie Settings Frequently Asked Questions and Solutions about Cookie Settings Jan 19, 2024 am 09:08 AM

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page Mar 20, 2024 pm 09:21 PM

Recently, many win10 users have found that their IE browser always automatically jumps to the edge browser when using computer browsers. So how to turn off the automatic jump to edge when opening IE in win10? Let this site carefully introduce to users how to automatically jump to edge and close when opening IE in win10. 1. We log in to the edge browser, click... in the upper right corner, and look for the drop-down settings option. 2. After we enter the settings, click Default Browser in the left column. 3. Finally, in the compatibility, we check the box to not allow the website to be reloaded in IE mode and restart the IE browser.

See all articles