Javascript 如何在浏览器关闭时删除 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4985693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:05:08  来源:igfitidea点击:

How do I remove cookies when the browser is closed?

javascriptcookies

提问by Osama khodrog

Possible Duplicate:
Clear cookies on browser close

可能的重复:
在浏览器关闭时清除 cookie

When I close the browser, I want to remove cookies. How can I do this?

当我关闭浏览器时,我想删除 cookie。我怎样才能做到这一点?

回答by Nick

If you don't set an expiry date it defaults to expire at the end of the session . Refer this links -

如果您未设置到期日期,则默认为在会话结束时到期。请参阅此链接 -

jscookies

jscookies

回答by ayush

taken from here

取自这里

function del_cookie(name) {
    document.cookie = name +
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }

Now all you need to do is to call this del_cookie()function passing it the name of whatever cookie it is that we wish to delete. The function will update the expiry date on the cookie to one long in the past so that the cookie will be considered to be expired and will be ignored by the browser exactly the same as if it didn't exist

现在你需要做的就是调用这个del_cookie()函数,传递给它我们想要删除的任何 cookie 的名称。该函数会将 cookie 上的过期日期更新为过去的一个 long,以便 cookie 将被视为已过期并被浏览器忽略,就像它不存在一样

to be used something like

使用类似的东西

<body onload="SetCookie()" onunload="del_cookie()">

回答by Oddthinking

You are looking for session cookies.

您正在寻找会话 cookie

A tutorial on hwo to create them in JavaScript is available here.

此处提供了有关在 JavaScript 中创建它们的 hwo 教程。