Reputation: 1427
document.cookie
gives me something like:
"cookie_name_1=623222.88839338.818181.383; SnapABugHistory=4#; optimizelyEndUserID=aaa1111111; my_cookie_name=%5data%7Bfrom%3Arails"
I only want to remove the my_cookie_name=%5data%7Bfrom%3Arails
and leave the rest intact.
What's the best way to do this from javascript?
Upvotes: 0
Views: 329
Reputation: 123503
Set it with a past expires
date -- simplest is probably the unix epoch (just new Date(0)
):
document.cookie = 'my_cookie_name=; expires=' + new Date(0).toUTCString();
This will only affect the cookie of the name given at the start of the string. To affect the other 3 cookies, you would have to repeat for each:
document.cookie = 'cookie_name_1=; expires=' + new Date(0).toUTCString();
document.cookie = 'SnapABugHistory=; expires=' + new Date(0).toUTCString();
//...
That's because the getter and setter of document.cookie
do differ. While you get a concatenated list of all available cookies, you can only set one at a time.
See the MDC docs for more details.
Upvotes: 1
Reputation: 8814
Take a look at Quirks Mode post about cookies. It will give you a great explanation about how to use cookies.
Near the end (section The scripts) there are three functions that you might find useful, namely createCookie(name,value,days)
, readCookie(name)
and eraseCookie(name)
, wich is exactly what you want.
Of course, this is pure javascript. If you're using a js framework (such as jQuery), you're better of using the framework's built-in functions
Upvotes: 0
Reputation: 1476
http://plugins.jquery.com/project/Cookie
Allows you to set/get cookies by name, use it to set my_cookie_name to 0 or ""
Upvotes: 0