我找到了一个我正在审查的 cookie 脚本,它可以创建 cookie 但不会杀死 cookie。
代码:
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
function KillCookie(cookieName) {
SetCookie(cookieName,"",-3);
}
function ReadCookie(cookieName) {
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName+"=");
if (ind==-1 || cookieName=="") return "";
var ind1=theCookie.indexOf(";",ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
如您所见,我创建了函数 KillCookie,它应该将过期日期设置回三天并让浏览器自动删除。问题是它没有删除它。
我在我的代码中编写了这些函数,如下所示
$(function() {
$('#left').before('<div id="left_widg"><button></button></span>');
$('#right').before('<div id="right_widg"><button></button></span>');
$('#left_widg button').on('click',function() {
var _checkme = $('#left').css('display');
var oriWidth = 180;
if(_checkme === "block") {
SetCookie('closeWidgetsLeft', 'true', 100);
$('#left').animate({width:'-='+ oriWidth +'px'},500,function() {
$(this).hide();
$('#left_widg button').html('Open');
});
} else {
KillCookie('closeWidgetsRight');
$('#left').show();
$('#left').animate({width:'+='+ oriWidth +'px'},500,function() {
$('#left_widg button').html('Close');
});
}
});
if (ReadCookie('closeWidgetsLeft') == 'true') {
$('#left').css('display','none');
} else if (ReadCookie('closeWidgetsRight') == 'true') {
$('#right').css('display','none');
}
});