Skip to content

Instantly share code, notes, and snippets.

@mholtzhausen
Created June 22, 2021 14:00
Show Gist options
  • Save mholtzhausen/bae0fc71fd0d7ad4acc40c609bf899a6 to your computer and use it in GitHub Desktop.
Save mholtzhausen/bae0fc71fd0d7ad4acc40c609bf899a6 to your computer and use it in GitHub Desktop.
Clear all Cookies on Primary domain and Subdomains

This function will remove all cookies that are:

  1. on the primary domain
  2. on the wildcard primary domain
  3. with the matching path (defaults to no-path or /)
  4. that is not an http cookie
function clearCookies(wildcardDomain=false, primaryDomain=true, path=null){
pathSegment= path ? '; path=' + path : ''
expSegment="=;expires=Thu, 01 Jan 1970 00:00:00 GMT"
document.cookie.split(';').forEach(
function(c) {
primaryDomain && (document.cookie = c.replace(/^ +/, "").replace(/=.*/, expSegment + pathSegment))
wildcardDomain && (document.cookie = c.replace(/^ +/, "").replace(/=.*/, expSegment + pathSegment + '; domain=' + document.domain))
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment