JS
October 24, 2022
In JS, how to remove all cookies and disable them?
For situations, like, for example, a user withdraws their consent to use cookies, you can delete all existing cookies:
function deleteCookies() { var theCookies = document.cookie.split(';'); for (var i = 0 ; i < theCookies.length; i++) { document.cookie = theCookies[i].split('=')[0] + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; } }
And then disable them by overriding a getter and setter:
if(!document.__defineGetter__) { Object.defineProperty(document, 'cookie', { get: function(){return ''}, set: function(){return true}, }); } else { document.__defineGetter__("cookie", function() { return '';} ); document.__defineSetter__("cookie", function() {} ); }