When navigating the vast digital realm, cookies serve as tiny data packets that websites store on your computer or device. They aid in remembering your browsing preferences and enhancing your user experience. However, at times, you may wish to clear all cookies for a specific domain, perhaps due to privacy concerns or troubleshooting purposes.
To obliterate all cookies associated with the current domain using JavaScript, you can employ the following code:
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 code effectively loops through each cookie stored in the document.cookie property, using the ; character as a separator. For each cookie, it extracts the cookie name and overwrites it with an expiration date set in the distant past. By doing so, the cookies are effectively purged from your system.
While this code is generally effective, it's essential to be aware of the following limitations:
The above is the detailed content of How Can JavaScript Delete All Cookies from the Current Domain?. For more information, please follow other related articles on the PHP Chinese website!