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

javascript - JQuery and Cookie: How to remove them when user leaves the page or exit windowtab - Stack Overflow

programmeradmin8浏览0评论

I am learning to use this JQuery cookie plugin at

From what I have read, it's really easy to read and remove a cookie. My question is: I want the cookie to be removed when the user navigates away from the page or close the window/tab, how do I do that? How do I detect such activity?

Thanks.

I am learning to use this JQuery cookie plugin at https://github./carhartl/jquery-cookie

From what I have read, it's really easy to read and remove a cookie. My question is: I want the cookie to be removed when the user navigates away from the page or close the window/tab, how do I do that? How do I detect such activity?

Thanks.

Share Improve this question edited Aug 10, 2015 at 11:45 Jaimin 8727 silver badges15 bronze badges asked Apr 18, 2013 at 18:52 neoneo 2,4719 gold badges44 silver badges67 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
$(window).on('beforeunload', function () {
    // Remove the cookie
    $.cookie("name_of_cookie", null);
});

Also, we have one more option:

$(window).unload(function () {
    // Remove the cookie
    $.cookie("name_of_cookie", null);
});

The update to the accepted answer would be:

Cookies.remove('name');

With: https://github./js-cookie/js-cookie

The jQuery plugin is not longer mantained.

发布评论

评论列表(0)

  1. 暂无评论