Home >Web Front-end >JS Tutorial >How Can I Delete All Cookies Using JavaScript?
In the realm of web development, the ability to manage cookies is pivotal. JavaScript, a versatile web scripting language, provides a powerful mechanism to remove all cookies associated with the current domain.
The JavaScript code below efficiently deletes every cookie within the current domain:
function deleteAllCookies() { // Split the cookie string into an array of individual cookies document.cookie.split(';').forEach(cookie => { // Determine the name of the cookie const eqPos = cookie.indexOf('='); const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie; // Expire the cookie into oblivion document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; }); }
While this solution is effective in most scenarios, it's important to be aware of its limitations:
The above is the detailed content of How Can I Delete All Cookies Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!