Delete cookie in browser close not on refresh page

I have seen many posts regarding the same problem, but I am not getting the exact solution. I want to delete a cookie in the browser or close a tab using javascript. I made a cookie delete function and called the onbeforeunload event . But I saw that this event also triggered when a page refresh I don't want to delete the cookie on the refresh page. And I have seen in many posts that they detect link, F5 keypress event and form submit and prevent the onbeforeunload event. But then how about the refresh button click and hit enter on the url bar. Therefore, I believe that this is not an exact solution. so help me to solve this problem.

Additional info: I am creating a cookie using PHP and I want to delete this cookie in the browser.

+3


source to share


3 answers


Cookies are automatically deleted when you close your browser, unless you have specified a lifetime for the cookie.



+9


source


To delete all cookies when you close your browser use the following code

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




hope this solves your problem.

0


source


If you want to delete cookies in your browser, it is better to check if the cookie exists on page load and delete it.

0


source







All Articles