Skip to content Skip to sidebar Skip to footer

Why Can't I Expire A Cookie With Javascript (not Httponly)

I've got a cookie being set in a framework I'm developing within via JavaScript (the framework appears to be using https://github.com/carhartl/jquery-cookie). I'm developing within

Solution 1:

Old question, but I just ran into this and discovered the issue.

If you have a hostOnly cookie, do not specify the domain when you modify/expire it.

Most cookie handling libraries will auto-specify the domain if one is not provided, making it difficult, if not impossible, to edit a hostOnly cookie.

Solution 2:

I think there's a problem with how you're setting the expiration. Setting cookies with JavaScript requires a UTC/GMT format for the date. See this related answer:

Which date formats can I use when specifying the expiry date when setting a cookie?

Solution 3:

You can't actually delete a cookie via javascript. What you do is set the existing cookie to expire and then allow the browser to handle its destruction. If you check the jquery-cookie source you can see that it actually has a function to destroy cookies that you could use here to simplify things.

The simple answer here is to use the existing frameworks remove function.

$.removeCookie(key)

Post a Comment for "Why Can't I Expire A Cookie With Javascript (not Httponly)"