admin管理员组

文章数量:1328037

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.

本文标签: javascriptJQuery and Cookie How to remove them when user leaves the page or exit windowtabStack Overflow