TIL how to delete all cookies with Javascript
POSTED ON:
TAGS: javascript cookies
I was building a plugin to add a Consent Bar because of the California Consumer Privacy Act (CCPA).
If someone selects 'Reject', you want to remove all the tracking cookies.
You can easily clear all cookies stored in a web page by accessing the cookie using document.cookie and clearing it.
const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));
via 20 Killer JavaScript One Liners
Related TILs
Tagged: javascript