日期:2014-05-16 浏览次数:20488 次
?
function getUsernameInCookie() {
var strcookie = document.cookie;
var arrcookie = strcookie.split("; ");
for ( var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
if (arr[0] == 'userInfo' && arr[1] != undefined) {
if (arr[1].split(escape(","))[0] == 'GoodBye') {
return '';
} else {
return arr[1].split(escape(","))[0];
}
}
}
return '';
}
function delCookie() {
var date = new Date();
date.setTime(date.getTime() - 2 * 3600 * 1000 * 24);
document.cookie = 'userInfo=GoodBye; expire=' + date.toGMTString();
}
function addCookie(name, value, expireHours) {
var cookieString = name + "=" + escape(value);
if (expireHours > 0) {
var date = new Date();
date.setTime(date.getTime + expireHours * 3600 * 1000 * 24);
cookieString = cookieString + "; expire=" + date.toGMTString();
}
document.cookie = cookieString;
}
function logout() {
delCookie();
$('#UserInfo').html("Welcome guest ");
window.location.href = "user/logout.action";
changeText();
}
?
?用户登陆后,通过js向浏览器中写入一个会话cookie,再登出时无论在ie8,还是chrome中都没问题。
?
因登陆的jsp中有这样的一段
?
$(document).ready(function() {
document.onkeydown = keyDown;
if (getUsernameInCookie() != '') {
$('#UserInfo').html(
"Welcome " + getUsernameInCookie()
+ " |");
changeText();
}
;
});
?再加之用户注册激活后为增加用户体验,所以自动登陆,只能在后台(Action)中向客户端传入cookie:
?
Cookie cookie = new Cookie("userInfo", DBusername);
cookie.setPath("/");
response.addCookie(cookie);
?如这样,在chrome中将cookie中的时间-1可以,但在ie8中用户名还是没办法清掉,就是这句:
?
?
date.setTime(date.getTime() - 2 * 3600 * 1000 * 24); document.cookie = 'userInfo=; expire=' + date.toGMTString();?
?
所以在最上面的js中加入了‘Goodbye’当标识符用。哎~~~
谁有好的方法欢迎拍砖!