如何在浏览器关闭的时候在jquery中删除cookie? [英] how to delete cookie in jquery at the time of browser closing?
问题描述
我已经使用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
andwindow.beforeunload
events, write a function to delete necessary cookies and attach to these events.这篇关于如何在浏览器关闭的时候在jquery中删除cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!