Clearing all cookies with JavaScript
javascriptYou can clear all cookies using JavaScript by iterating through all cookies using the document.cookie property and deleting each one individually. Here’s an example:
function clearAllCookies() {
// Split cookie string and remove all cookies
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
In this example, we define a function called clearAllCookies() that iterates through all cookies using the document.cookie property. For each cookie, we extract the name and set its expiration date to a date in the past, effectively deleting it. Finally, the function can be called whenever you need to clear all cookies, for example:
// Call clearAllCookies() function to clear all cookies
clearAllCookies();
Note that this method will only clear cookies for the current domain. If you want to clear cookies for a specific domain, you can include the domain parameter in the expires attribute, like this:
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=mydomain.com";
Replace mydomain.com with the domain for which you want to clear cookies.
Other Article on Tag javascript
- - Adding and removing content in jQuery
- - Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
- - Can I change the Dojo namespace to something other than dojo
- - Can I prevent user pasting Javascript into Design Mode IFrame
- - dynamically create html element in javascript
- - Find XY of an HTML element with JavaScript
- - How can I upload files asynchronously with jQuery
- - How can you display Typing Speed using Javascript or the jQuery library
- - How do you capture mouse events in firefox
- - How to auto-size an iFrame