我有一个问题。我试图清除标签关闭时的cookie值。我已经创建了一个函数但它也清除了页面重载的价值。我也试过这个解决方案stack-overflow question
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";
}
function unloadPage()
{
var check=getCookie('mycookie')
if(check)
setCookie('mycookie','','-2');
}
$(document).ready(function() {
window.onbeforeunload = unloadPage;
});
功能正在运行,但问题是它还在页面重新加载时清除cookie 。但是我只想在用户关闭窗口时清除cookie。
感谢。