How to Delete All Cookies for the Current Domain Using JavaScript?
Nov 27, 2024 am 04:33 AMDeleting All Cookies for Current Domain with JavaScript
In JavaScript, deleting all cookies for the current domain involves programmatically removing them from the browser's storage.
How to Delete All Cookies Using JavaScript
To clear all cookies for the current domain, you can use the following JavaScript function:
function deleteAllCookies() { document.cookie.split(';').forEach(cookie => { const eqPos = cookie.indexOf('='); const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie; document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; }); }
This function iterates through all cookies stored in the document's cookie property, extracting their names and setting them with an expiration date in the past (January 1, 1970), effectively "deleting" them.
Limitations of the Approach
Note that this code has some limitations:
- It cannot delete cookies with the HttpOnly flag set, which prevents JavaScript from accessing them.
- It cannot delete cookies set with a specific Path value and requires matching the Path value used when setting the cookie.
The above is the detailed content of How to Delete All Cookies for the Current Domain Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Replace String Characters in JavaScript

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial
