java 删除cookie不关闭浏览器,关闭浏览器时会删除cookie

博客讨论了在不同浏览器中实现Cookie持久化的问题。作者提到,使用当前的setCookie函数,当用户关闭Chrome和Firefox时,Cookie会被删除。问题在于函数未设置正确的过期日期。解决方案是为Cookie设置一个未来的过期时间,如设置为true,以确保它在浏览器关闭后仍然存在。
摘要由CSDN通过智能技术生成

I have this cookie to store users previous selection from a menu. But I want keep this cookie stored in the browser even if the user closes the browser. This code perfectly works on IE but when I close Chrome and Firefox, the cookie getting deleted. Can anyone provide me solution for this.

function setCookie(NameOfCookie, value, expiredays) {

var ExpireDate = new Date();

document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());

}

function delCookie (NameOfCookie) {

if (getCookie(NameOfCookie)) {

document.cookie = NameOfCookie + "=" + "; expires=Thu, 17-Jul-24 00:00:01 GMT";

}

}

解决方案

If you call the function with

setCookie('redirectcountry', 'CA')

then you set the expiry date to new Date(), that is "right now". So it's normal behavior to have the cookie be immediately deleted.

Simple solution : call it as

setCookie('redirectcountry', 'CA', true)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值