Home > Mobile >  Deleting cookie via browser vs via Javascript
Deleting cookie via browser vs via Javascript

Time:08-25

What is the difference between deleting cookies via the browser e.g. by using this little menu in Edge :

cookies browser

and setting max age to -1 via Javascript, e.g. with code like this :

document.cookie = "MyCookie=; max-age=-1; path=/;domain=mydomain.com"

The context : I'm using some internal service in a company which should log a user out, but it requires me to remove cookies first. When using the former method (manual removal) it works, when using Javascript it doesn't. I've tried various combinations of paths, domains, max-age or expiration dates.

CodePudding user response:

Deleting cookie from browser via settings or Dev tools will remove all cookies (including "HTTP only" cookie), while document.cookie cannot delete "HTTP only" cookies.

If a cookie is set with "HTTP Only" flag, it cannot be accessed by JavaScript. In your case, Your session cookie might have "HTTP only" flag, that's why its not getting deleted when you are trying to delete it with document.cookie.

You can view this from your browser Dev tools (while you are logged in). To remove HTTP only cookie, you can update its value and expiry via HTTP response (similar to how you set the cookie at first place)

Set-Cookie: MyCookie=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT

  • Related