admin管理员组文章数量:1353127
We are trying to clear the cookie details in browser on pressing 'Logout' button with the following code, but the script doesn't remove the session cookie from the browser. But by clearing the session cookies in IE8 browser using developer tool(Tools > Developer Tools >Cache > Clear Session Cookies), the cookies are cleared.
<html:link page="/home.do" onclick="logout();">
<html:img page="/images/logout.jpg"/>
</html:link>
function logout(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookiename = cookies[i].split("=");
var d = new Date();
d.setDate(d.getDate() - 4);
var expires = ";expires="+d;
var value="";
document.cookie = cookiename + "=" + value + expires + ";";
}
}
How to clear the Session cookies from the browser using script?
We are trying to clear the cookie details in browser on pressing 'Logout' button with the following code, but the script doesn't remove the session cookie from the browser. But by clearing the session cookies in IE8 browser using developer tool(Tools > Developer Tools >Cache > Clear Session Cookies), the cookies are cleared.
<html:link page="/home.do" onclick="logout();">
<html:img page="/images/logout.jpg"/>
</html:link>
function logout(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookiename = cookies[i].split("=");
var d = new Date();
d.setDate(d.getDate() - 4);
var expires = ";expires="+d;
var value="";
document.cookie = cookiename + "=" + value + expires + ";";
}
}
How to clear the Session cookies from the browser using script?
Share Improve this question edited Sep 7, 2012 at 7:48 CharlesB 90.6k29 gold badges201 silver badges228 bronze badges asked Sep 7, 2012 at 7:37 UviUvi 411 gold badge1 silver badge2 bronze badges 2-
Why do you want to remove the cookie? Call
session.invalidate()
would be enough, the cookie will be deleted on browser exit anyway. – f_puras Commented Sep 7, 2012 at 7:40 - 1 possible duplicate of Clearing all cookies with JavaScript – tchrist Commented Sep 9, 2012 at 23:52
3 Answers
Reset to default 3if it is httpOnly, then you can not delete it, try modify it from server side
from javascript: Not specifying expiring field, also set value to null, and if it is also hostonly, not specifying domain
If it is a 'session' variable you want to clear...
unset( $_SESSION['YOUR_SESSION_VARIABLE_NAME'] );
This will pletely remove this session variable. The title is misleading. The code he shows is not 'session' cookies, they are regular 'cookies'.
本文标签: javascriptHow to remove session cookieStack Overflow
版权声明:本文标题:javascript - How to remove session cookie - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743913125a2560756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论