最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete cookie on browser close not on page refresh - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 13

Cookies 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.

发布评论

评论列表(0)

  1. 暂无评论