即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

js 清空当前网址一切cookie

编程语言 gj333 10℃ 0评论

网上找了些代码,有些成功率不高。修改了一些,测试成功率可以99%。


function foreach()
{
  var strCookie=document.cookie;
  var arrCookie=strCookie.split("; ");
  for(var i=0;i0)
    DelCookie(arr[0]);
 }
}
function GetCookieVal(offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return decodeURIComponent(document.cookie.substring(offset, endstr));
}
function DelCookie(name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
function GetCookie(name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function clearCookie(){
    var date=new Date();
    date.setTime(date.getTime()-10000);
    var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
    if (keys) {
        for (var i =  keys.length; i--;)
          document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
    }
}
clearCookie();





转载请注明:CodingBlog » js 清空当前网址一切cookie

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情