如何在浏览器关闭的时候在jquery中删除cookie? [英] how to delete cookie in jquery at the time of browser closing?

查看:747
本文介绍了如何在浏览器关闭的时候在jquery中删除cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用jQuery创建了一个cookie。当浏览器关闭时,我想使用jQuery删除cookie。



我写了以下脚本:

  $(function(){
if($ .cookie('toggle')==toggle){
$('#toggleText')。show();
$('#displayText').text(Less);
}
else {
$('#toggleText')。hide();
$(' displayText')。text(More);
}
$('#displayText')。 更多){
$('#toggleText')。show();
$(this).text(Less);
$ .cookie toggle;);

} else {
$('#toggleText')。hide();
$(this).text b $ .cookie('toggle','nottoggle');

}
}); b
$ b})



当用户刷新浏览器时,也不应删除cookie。



谢谢。

解决方案

如果您在关闭浏览器时想要删除Cookie,则应设置会话Cookie ,在浏览器关闭时会自动过期。但是如果你想要关闭一个标签页,而不是你应该使用 window.unload window.beforeunload 事件时删除cookie,写一个函数来删除必要的cookies并附加到这些事件。


I have created a cookie by using jQuery. I want to delete the cookie with jQuery when the browser closes. How do I do this?

I've written the following script:

$(function () {
         if ($.cookie('toggle') == "toggle") {
             $('#toggleText').show();
             $('#displayText').text("Less");
         }
         else {
             $('#toggleText').hide();
             $('#displayText').text("More");
         }
         $('#displayText').click(function () {
             if ($(this).text() == "More") {
                 $('#toggleText').show();
                 $(this).text("Less");
                 $.cookie('toggle', 'toggle');

             } else {
                 $('#toggleText').hide();
                 $(this).text("More");
                 $.cookie('toggle', 'nottoggle');

             }
         });

     })

The cookie should also not be deleted when the user refreshes the browser.

Thanks.

解决方案

If you want delete cookie when you close browser than you should set a session cookie, which automatically expires when browser closed. But if you want delete cookie when you close a tab than you should use window.unload and window.beforeunload events, write a function to delete necessary cookies and attach to these events.

这篇关于如何在浏览器关闭的时候在jquery中删除cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆