简体   繁体   English

使用javascript / jQuery删除cookie

[英]remove cookie using javascript / jQuery

I have a little demo page to show the effect of a website depending on different user cookies. 我有一个小样的演示页面来显示网站的效果,具体取决于不同的用户Cookie。 Then I set the click() function of some div to use the plugin jquery.cookie.js which provides 2 functions: 然后,将某些divclick()函数设置为使用插件jquery.cookie.js,该插件提供2个函数:

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

after I called $.removeCookie() , I call window.open('new page') since I need to go to the content. 在我调用$.removeCookie() ,我调用window.open('new page')因为我需要转到内容。 but httpliveheader always shows that it's not removing the cookie in question. 但是httpliveheader总是显示它没有删除有问题的cookie。

OK, I found it, it was because the cookie set by my backend code and front end JS are in different path. 好的,我找到了它,是因为我的后端代码和前端JS设置的cookie位于不同的路径。

my java spring MVC controller has an access path of /MyPath/Blah/ in the code I just did 我的java spring MVC控制器在我刚刚执行的代码中的访问路径为/ MyPath / Blah /

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

this turns out to default to the path where the code sits under , ie /MyPath/Blah/ 原来默认为代码所在的路径,即/ MyPath / Blah /

but the JS sets something like $.cookie("something", "somevalue"), it goes to root. 但是JS设置了类似$ .cookie(“ something”,“ somevalue”)之类的东西,它进入了根目录。 that's why I am seeing 2 different values in httpLiveHeader dump. 这就是为什么我在httpLiveHeader转储看到2个不同的值。 unfortunately the path thing is controlled by browser, so it doesn't show up on liveheader dump. 不幸的是,路径的事情是由浏览器来控制,因此它不会对liveheader转储显示。 I only found this after I inspected the "remove cookie" window in mozilla 我只找到这个,我考察了Mozilla的“删除cookie的”窗口后,

//for example : //例如 :

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

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

                          "; path=/";

you need to set PATH. 您需要设置PATH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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