我不确定我的问题是否与this one有关。
IE9在关闭浏览器后会删除此cookie(预期)但Chrome 12,Firefox 5和Opera 11则不会。 (在测试下面的示例时,单击“删除帐户”后,每个浏览器都被关闭。然后在短时间内重新打开它们,除了IE9之外,所有的浏览器仍然存在。)
使用案例 Cookie会在用户上次访问后1年到期。帐户删除应删除cookie。
问题:
(1/2)为什么IE9做正确(预期)的事情而其他人不做?
(2/2)如何确保所有浏览器都销毁此cookie?
示例:
的login.html
<!doctype html>
<html>
<head>
<title>Create Cookie Example</title>
<script>
function setCookie() {
var expDate = new Date();
expDate.setDate(expDate.getDate() + 365);
document.cookie = "fakeCookie=" + escape("fake value")
+ "; expires=" + expDate.toGMTString();
}
</script>
</head>
<body onload="setCookie()">
<h1>Welcome</h1>
<p>Lorem ipsum...</p>
<hr size="1" />
<p><a href="profile.html">User Profile</a></p>
</body>
</html>
profile.html
<!doctype html>
<html>
<head>
<title>Delete Cookie Example</title>
<script>
function deleteConfirm() {
if ( confirm("Are you sure you want to delete your account? "
+ "All data will be lost; this action cannot be undone!")
) deleteConfirmed()
else return false
return true;
}
function deleteConfirmed() {
document.cookie = "fakeCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
</script>
</head>
<body>
<h1>User Profile</h1>
<p>Lorem ipsum...</p>
<hr size="1" />
<p><a href="index.html" onclick="return deleteConfirm()">Delete Account</a></p>
</body>
</html>
编辑:原始帖子错误地将login.html识别为index.html(形成一个循环引用,在删除“帐户”时重新创建cookie。)
答案 0 :(得分:5)
OP提出了这个答案,最初将其编辑成问题。对于语义而言,这只是将解决方案保留在答案帖子中的转贴。
<script>
function deleteConfirm() {
if ( confirm("Are you sure you want to delete your account? "
+ "All data will be lost; this action cannot be undone!")
) deleteConfirmed(); // <-- ** MISSED SEMICOLON HERE **
else return false; // <-- ** AND HERE **
return true;
}
function deleteConfirmed() {
document.cookie = "fakeCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
</script>