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.
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.
Share Improve this question asked May 21, 2015 at 10:06 Dhaval RajaniDhaval Rajani 3111 gold badge3 silver badges15 bronze badges 3-
There is no "browser close", nor "page refresh" event.
onbeforeunload
event fires on both page (tab) close and page refresh. – hindmost Commented May 21, 2015 at 10:14 - @hindmost so no solution for browser close cookie delete? – Dhaval Rajani Commented May 21, 2015 at 10:26
- Hi, I have the same issue. Have you found a solution to this? – Jeff Commented May 25, 2016 at 14:07
3 Answers
Reset to default 13Cookies are automatically deleted when the browser is closed, unless you specify a lifetime for the cookie.
If you want to delete a cookie on browser close, better would be to check if cookie exists on page load and delete that.
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.