In JavaScript, you can delete a cookie with a specific domain by setting the domain property of the cookie to the domain you want to delete the cookie for and setting the expires property to a date in the past. Here’s an example:
// Delete a cookie with a specific domain
function deleteCookie(name, domain) {
// Set the domain property of the cookie
var domainString = domain ? "domain=" + domain + ";" : "";
// Set the expires property to a date in the past
document.cookie = name + "=; " + domainString + "expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
In this example, we’re defining a function called deleteCookie
that takes two parameters: the name of the cookie to delete and the domain of the cookie. If the domain parameter is not provided, the function will delete the cookie for the current domain.
To delete the cookie, we first construct a string that sets the domain property of the cookie using the specified domain (if provided). We then set the expires property to a date in the past to immediately expire the cookie. Finally, we set the path property of the cookie to /
to make sure the cookie is deleted for all paths within the domain.
To use this function, you can call it like this:
deleteCookie("myCookie", "example.com");
This will delete the myCookie
cookie for the example.com
domain. If you want to delete the cookie for the current domain, you can call the function like this:
deleteCookie("myCookie");
+ There are no comments
Add yours