简体   繁体   中英

remove cookie using javascript / jQuery

I have a little demo page to show the effect of a website depending on different user cookies. Then I set the click() function of some div to use the plugin jquery.cookie.js which provides 2 functions:

$.cookie('name', 'val')
$.removeCookie('name')

after I called $.removeCookie() , I call window.open('new page') since I need to go to the content. but httpliveheader always shows that it's not removing the cookie in question.

OK, I found it, it was because the cookie set by my backend code and front end JS are in different path.

my java spring MVC controller has an access path of /MyPath/Blah/ in the code I just did

httpServletResponse.addCookie(new Cookie("something", "something"));

this turns out to default to the path where the code sits under , ie /MyPath/Blah/

but the JS sets something like $.cookie("something", "somevalue"), it goes to root. that's why I am seeing 2 different values in httpLiveHeader dump. unfortunately the path thing is controlled by browser, so it doesn't show up on liveheader dump. I only found this after I inspected the "remove cookie" window in mozilla

//for example :

document.cookie = "cookie_name=" + encodeURIComponent(cookie_value) +

                          "; expires=" +  expires.toGMTString() +

                          "; path=/";

you need to set PATH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM