3

I seen many post regarding same problem but i am not getting exact solution. i want to delete cookie on browser or tab close event using javascript. I have made delete cookie function and called on onbeforeunload event. But i seen that event also called when page refresh i dont want to delete cookie on page refresh. And i seen in many post that they are detecting link click, keypress event of F5 and form submit and in that they preventing onbeforeunload event. But then what about refresh button click and press enter at url bar. So i think this is not a exact solution. so help me out from this problem.

Further information is i am creating cookie using PHP and want to delete this cookie on browser close.

Dhaval Rajani
  • 191
  • 1
  • 2
  • 15

3 Answers3

12

Cookies are automatically deleted when the browser is closed, unless you specify a lifetime for the cookie.

Maroun Baydoun
  • 454
  • 3
  • 8
  • you mean i will set cookie without expiry time?? – Dhaval Rajani May 21 '15 at 10:11
  • Yes exactly. Without expiry time, the cookies are deleted on browser close. – Maroun Baydoun May 21 '15 at 10:13
  • i am setting cookie using below code $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day*/ can you tell me how to set this cookie without expiry time – Dhaval Rajani May 21 '15 at 10:14
  • What jquery cookies plugin are you using? – Maroun Baydoun May 21 '15 at 10:18
  • I am not using any plugin for this i have made following function for delete cookie function delete_cookie( name, path, domain ) { document.cookie = name + "=" + ((path) ? ";path="+path:"")+ ((domain)?";domain="+domain:"") + ";expires=Thu, 01 Jan 1970 00:00:01 GMT"; } – Dhaval Rajani May 21 '15 at 10:21
  • Simply remove the ``expires`` value from the function – Maroun Baydoun May 22 '15 at 01:21
  • they are called session cookies – Lucky Soni Aug 18 '16 at 13:44
  • Suppose I do close the browser but just close that particular browser "tab", will the cookie for the application of that tab be deleted? Will this behavior be browser specific? – hagrawal7777 Mar 15 '17 at 14:16
  • I'm leaving out the expire date and the cookie isn't set at all, I've tested this in both newest Firefox and Safari and it's the same behaviour ... – bork Apr 11 '18 at 10:13
  • Unfortunately, this is no longer true. https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies – wortwart Nov 20 '18 at 14:20
0

To delete all the cookies when browser close uses the following code

$(window).bind('beforeunload', function(event) {    
    var cookies = $.cookie();
      for(var cookie in cookies) {
        $.removeCookie(cookie);
      }
    return true;
});

Hope this will solve your problem.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
0

If you want to delete a cookie on browser close, better would be to check if cookie exists on page load and delete that.

  • my concept is i am saving product id in cookie when i am saving product as my favourite product without login and i want to delete this cookie on close browser not on page refresh your solution will delete my cookie on page refresh also. – Dhaval Rajani May 21 '15 at 12:10