Deleting 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:
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!