0

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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
teddy teddy
  • 3,025
  • 6
  • 31
  • 48
  • Put in an alert after the `$.removeCookie` to make sure it's being called without any errors. – Flight Odyssey Nov 01 '13 at 01:30
  • See also: http://stackoverflow.com/questions/3671659/jquery-delete-cookies – Flight Odyssey Nov 01 '13 at 01:31
  • 2
    Can you provide your code and/or a link to a JSFiddle? – Justin Russo Nov 01 '13 at 01:31
  • `$.removeCookie()` requires an argument... (based on code you've posted, if you have posted just function name it is unlikely to get meaningful help). Please consider debugging and posting error messages if you see any. – Alexei Levenkov Nov 01 '13 at 01:34
  • technically it is impossible to actually delete a cookie, jQuery just sets the expire date to a time in the past, if that helps at all – markasoftware Nov 01 '13 at 01:35
  • thanks guys, I did go through firebug and saw that the $.removeCookie() line is called. – teddy teddy Nov 01 '13 at 15:29
  • I did the following : document.cookie = "buyerId=11";//";expires=Thu, 01-Jan-70 00:00:01 GMT;"; but ended up getting "Cookie: buyerId=oldValue; someOtherCookie; buyerId=11 " in httpliveheader – teddy teddy Nov 01 '13 at 20:06

2 Answers2

0

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 , i.e. /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

teddy teddy
  • 3,025
  • 6
  • 31
  • 48
-1

//for example :

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

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

                          "; path=/";

you need to set PATH.

Arnold
  • 88
  • 1
  • 3
  • path is optional in cookies https://developer.mozilla.org/en-US/docs/Web/API/document.cookie Also OP is using jQuery cookie plugin that knows if you don't set path how to handle it – charlietfl Nov 01 '13 at 01:41
  • i know,you can't use jQuery cookie,need to rewrite.you can look at here http://www.quirksmode.org/js/cookies.html ,about path in cookie – Arnold Nov 01 '13 at 01:54