0

I have one function use for writing cookie :

function Search_Click() {
  $.cookie("urlAction", null);
  $.cookie("searchKey", null);
  var searchkey = $('#txtsearch').val();
  var key = decodeURIComponent(searchkey);
  sampleData = "Q=" + key;
  if ($.cookie("urlInStock") != null) {
    action = "NewSearchIn";
  } else {
    action = "NewSearch";
  }
  urlAction = "Pro/" + action + "/";
  $.cookie("searchKey", sampleData);
  $.cookie("urlActionVal", urlAction);
  window.location = "/Pro?tab=2";
}

After Search_Click() is done, the page will redirect to another page with the cookie that I just registered.

Then if a user click on a link, I would like to delete that cookie.

This is what I tried :

 function deleteSearchCookies() {
    $.cookie("searchKey", null);
    $.cookie("urlActionVal", null);
 }

But when I bug in FireBug, the session still exist.

Nothing
  • 2,588
  • 11
  • 52
  • 109

3 Answers3

3
function deleteCookie(name) {
    document.cookie = name+'="";-1; path=/';
}

var login = document.getElementById("loginlink");
login.onclick = function() {
  deleteCookie("name");
};
Prabu Parthipan
  • 3,673
  • 2
  • 16
  • 25
2

To delete:

function deleteSearchCookies() {
    $.removeCookie("searchKey");
    $.removeCookie("urlActionVal");
}

For more info visit question

if you don't want to use plugin, use (its alternate modify as you wish)

//Creates client side cookie
function Cookie(name, value, minutes) {
   var expires = "";
   if (minutes) {
      var date = new Date();
      date.setTime(date.getTime() + (minutes * 60 * 1000));
      expires = "; expires=" + date.toGMTString();
   }

   document.cookie = name + "=" + value + expires;
}

function deleteSearchCookies() {
    Cookie("searchKey",null,-1);
    Cookie("urlActionVal",null,-1);
}
Community
  • 1
  • 1
Satpal
  • 126,885
  • 12
  • 146
  • 163
1

delete like this:

$.cookie(cookieName, null, { expires: -1, path: '/' }); 

Sorry you need to save it with same path like this:

var date = new Date();
date.setTime(date.getTime() + sameValueForTime);
$.cookie(cookieName, value, { expires: date, path: '/' });

Sometimes firebug bugs and shows that cookie is there when it is deleted, but you can only see it in cookie area not use it.

Vladimir Bozic
  • 434
  • 6
  • 16