日期:2014-05-16  浏览次数:20404 次

js怎么都删不了cookie,谁能帮调试一下。
cookie.js
JScript code

var cookie_pre = 'sYQDUGqqzH';
var cookie_domain = '';
var cookie_path = '/';
function getcookie(name) {
    name = cookie_pre+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 setcookie(name, value, days) {
    name = cookie_pre+name;
    var argc = setcookie.arguments.length;
    var argv = setcookie.arguments;
    var secure = (argc > 5) ? argv[5] : false;
    var expire = new Date();
    if(days==null || days==0) days=1;
    expire.setTime(expire.getTime() + 3600000*24*days);
    document.cookie = name + "=" + escape(value) + ("; path=" + cookie_path) + ((cookie_domain == '') ? "" : ("; domain=" + cookie_domain)) + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
}

function delcookie(name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = getcookie(name);
    name = cookie_pre+name;
    document.cookie = name+"="+cval+";expires="+exp.toGMTString();
}

function getcookieval(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if(endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}



index.html

HTML code



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" class="addbg">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>phpcmsV9 - 后台管理中心</title>
<script type="text/javascript" src="cookie.js"></script>
<script>
alert(getcookie('mytest'));
function add(name,v){
    setcookie(name,v,1);
}
function dle(name){
    delcookie(name,v,1);
}
</script>
</head>
<body>
<input type="button" value="addcookie" onclick="add('mytest','aaaa')"  />

<input type="button" value="dlecookie"  onclick="dle('myuser');" />
</body>
</html>


就是不按套路来。

------解决方案--------------------
add('mytest'

dle('myuser')


你从哪里观察cookie的?
------解决方案--------------------
function delcookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = getcookie(name);
name = cookie_pre+name;
document.cookie = name+"="+cval+";expires="+exp.toGMTString();
}
这里只有一个参数。。而你这里function dle(name){
delcookie(name,v,1);
}为什么又传3个参数呢?没搞懂
------解决方案--------------------
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";