gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
function clearAllCookies(
- User
function clearAllCookies(
- Assistant
Here's a JavaScript function that clears all cookies for the current domain:
javascriptfunction clearAllCookies() { const cookies = document.cookie.split(";"); for (let cookie of cookies) { const eqPos = cookie.indexOf("="); const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim(); // Setting the cookie expiration date in the past to delete it document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/"; } }Explanation:
document.cookiereturns a string of all cookies separated by;.- We split them into individual cookies.
- For each cookie, extract its name.
- Set its expiration date to a past date, which effectively deletes the cookie.
- The
path=/ensures the cookie is deleted across the entire domain path.