gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
JS clear cookies
- User
JS clear cookies
- Assistant
To clear cookies using JavaScript, you typically delete each cookie by setting its expiration date to a past date. JavaScript does not provide a direct method like
clearCookies(), so you need to iterate over all cookies and remove them manually.Here’s a basic example to clear all cookies for the current page:
function clearCookies() { 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(); // Set the cookie with an expiration date in the past document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/"; } } clearCookies();Important notes:
- This clears cookies accessible via JavaScript and on the current path/domain.
- Cookies with
HttpOnlyflag cannot be deleted via JavaScript for security reasons. - If cookies were set with specific domain or path attributes, you might need to specify those when deleting.