How to delete a cookie from a specific domain using Javascript?

How to delete a cookie from a specific domain using Javascript?

Deleting a cookie from a specific domain using JavaScript involves setting the cookie's expiry to a past date. Here's how you can achieve this:

Step-by-Step Solution:

  1. Find the Cookie Name: Identify the name of the cookie you want to delete.

  2. Set Expiry Date: Set the expiry date of the cookie to a past date. This effectively removes the cookie by making it expire immediately.

  3. Delete the Cookie: Use document.cookie to set the cookie with the updated expiry date.

Example Code:

Assume you want to delete a cookie named "exampleCookie" from the domain "example.com":

function deleteCookie(cookieName, domain) { // Set the cookie expiry date to the past document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=" + domain + "; path=/;"; } // Example usage: deleteCookie("exampleCookie", ".example.com"); 

Explanation:

  • Setting Expiry: By setting expires to a date in the past (Thu, 01 Jan 1970 00:00:00 UTC), the browser interprets this as the cookie having expired, thus deleting it.

  • Domain and Path: Specify the domain and path of the cookie you want to delete. This ensures that you are targeting the correct cookie.

  • Function Usage: Call deleteCookie("cookieName", "domain") with your specific cookie name and domain to delete the cookie.

Considerations:

  • Security: JavaScript can only delete cookies that it has access to, meaning cookies set by the same origin or subdomains.

  • Domain Matching: Ensure that the domain you specify matches the domain of the cookie exactly. If the cookie was set with a specific path (path=/path/) or secure flag (secure), those settings must also be included when deleting.

  • Browser Support: This method is widely supported across modern browsers and is the recommended way to delete cookies using JavaScript.

Examples

  1. JavaScript delete cookie by name

    Description: Delete a specific cookie by its name.

    // Example: Delete a cookie by name function deleteCookie(cookieName) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; } // Usage deleteCookie("cookieNameToDelete"); 

    This function sets the cookie's expiration date in the past, effectively deleting it.

  2. JavaScript delete cookie from specific domain

    Description: Remove a cookie from a specific domain.

    // Example: Delete a cookie from a specific domain function deleteCookieFromDomain(cookieName, domain) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=" + domain + "; path=/;"; } // Usage deleteCookieFromDomain("cookieNameToDelete", "example.com"); 

    Specify the domain in the cookie deletion function to remove it from that specific domain.

  3. JavaScript delete all cookies

    Description: Delete all cookies stored for the current domain.

    // Example: Delete all cookies for the current domain function deleteAllCookies() { 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 UTC; path=/;"; } } // Usage deleteAllCookies(); 

    This function iterates through all cookies and deletes each one by setting its expiration date to the past.

  4. JavaScript delete cookie from specific path

    Description: Remove a cookie from a specific path on the current domain.

    // Example: Delete a cookie from a specific path function deleteCookieFromPath(cookieName, path) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=" + path + ";"; } // Usage deleteCookieFromPath("cookieNameToDelete", "/specific/path"); 

    Specify the path where the cookie was set to delete it from that specific path.

  5. JavaScript delete cookie with secure flag

    Description: Delete a secure cookie.

    // Example: Delete a secure cookie function deleteSecureCookie(cookieName) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; secure"; } // Usage deleteSecureCookie("cookieNameToDelete"); 

    Set the secure flag when deleting a cookie to ensure it's only deleted when using a secure (HTTPS) connection.

  6. JavaScript delete cookie with specific expiration date

    Description: Delete a cookie by setting a specific expiration date.

    // Example: Delete a cookie with a specific expiration date function deleteCookieWithExpiration(cookieName, expirationDate) { document.cookie = cookieName + "=; expires=" + expirationDate + "; path=/;"; } // Usage var now = new Date(); var expirationDate = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000); // 1 day ago deleteCookieWithExpiration("cookieNameToDelete", expirationDate.toUTCString()); 

    Set a specific expiration date to delete the cookie at a designated time.

  7. JavaScript delete cookie by path and domain

    Description: Remove a cookie by specifying both path and domain.

    // Example: Delete a cookie by path and domain function deleteCookieWithPathAndDomain(cookieName, path, domain) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=" + path + "; domain=" + domain + ";"; } // Usage deleteCookieWithPathAndDomain("cookieNameToDelete", "/specific/path", "example.com"); 

    Specify both the path and domain where the cookie was set to delete it from that specific location.

  8. JavaScript delete HttpOnly cookie

    Description: Delete an HttpOnly cookie.

    // Example: Delete an HttpOnly cookie function deleteHttpOnlyCookie(cookieName) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; HttpOnly"; } // Usage deleteHttpOnlyCookie("cookieNameToDelete"); 

    Set the HttpOnly flag when deleting a cookie to ensure it's only deleted via HTTP requests.

  9. JavaScript delete cookie by secure and HttpOnly flags

    Description: Delete a cookie by specifying both secure and HttpOnly flags.

    // Example: Delete a cookie by secure and HttpOnly flags function deleteSecureHttpOnlyCookie(cookieName) { document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; secure; HttpOnly"; } // Usage deleteSecureHttpOnlyCookie("cookieNameToDelete"); 

    Ensure secure and HttpOnly cookies are deleted with the appropriate flags set.

  10. JavaScript delete cookie using regex

    Description: Delete a cookie matching a specific pattern using regular expressions.

    // Example: Delete a cookie using regex function deleteCookieByRegex(cookieRegex) { var cookies = document.cookie.split("; "); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i]; if (cookie.match(cookieRegex)) { var cookieParts = cookie.split("="); var cookieName = cookieParts[0]; document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; } } } // Usage deleteCookieByRegex(/cookiePattern.*/); 

    Use a regular expression to match and delete cookies based on a specific pattern.


More Tags

actionscript-3 footer react-testing-library gridextra karma-runner andengine xlwt smarty flutter-navigation linq-to-sql

More Programming Questions

More Housing Building Calculators

More Retirement Calculators

More Fitness Calculators

More Geometry Calculators