Home > Article > Web Front-end > How to Delete All Cookies Programmatically with JavaScript?
Deleting All Cookies with JavaScript
Question: How can I programmatically remove all cookies for the current domain using JavaScript?
Answer: JavaScript provides a method to delete all cookies stored by the browser for the current domain. Here's a function that accomplishes this:
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'; }); }
However, there are limitations associated with this technique:
By calling the deleteAllCookies() function, you can effectively clear all non-HttpOnly cookies for the current domain without specifying paths.
The above is the detailed content of How to Delete All Cookies Programmatically with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!