Answers
可以在页面
unload
方法里,用js清除cookie。
Cookie = {
"write":function(name, value, expires) {
if(expires == null) {
var d = new Date();
d.setTime(d.getTime() + 24 * 60 * 60 * 1000);
expires = d.toGMTString();
}
document.cookie = name + "=" + escape(value) + ";expires=" + expires;
},
"read":function(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if(arr!=null) {
return unescape(arr[2]);
}
return null;
},
"remove":function(name) {
var value = Cookie.read(name);
if(value!=null) {
document.cookie = name + "=" + value + ";expires=Fri, 02-Jan-1970 00:00:00 GMT";
}
}
}
Cookie.write("name", "dewen.org");//测试cookie,默认设置一天
//记录打开的地址数量
var host = location.host;
var pageCount = parseInt(Cookie.read(host));
Cookie.write(host, isNaN(pageCount) ? 1 : (pageCount + 1));
window.onunload = function() {
//判断打开的地址数量
var pageCount = parseInt(Cookie.read(host));
pageCount = isNaN(pageCount) ? 0 : (--pageCount);
if(pageCount == 0) {
//处理cookie失效
Cookie.remove(location.host);
Cookie.remove("name");//清除cookie
alert(Cookie.read("name"));
}else {
Cookie.write(locaion.host, pageCount);
}
}
朕射你无罪
answered 11 years, 10 months ago