function setCookie(name,value,hours,path){  
 var name = escape(name);  
var value = escape(value);  
var expires = new Date();  
expires.setTime(expires.getTime() + hours*3600000);  
path = path == "" ? "" : ";path=" + path;  
_expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();  
document.cookie = name + "=" + value + _expires + path;  
}
以上为设置一个值到cookie中, 删除的时候,
function delCookie(name){
// setCookie(name,"",{expireDays:-1});
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}  为什么这个方法delCookie()没有用呢,
 我在网上找的代码都不能删除很是郁闷, 
请各位帮我写个删除cookie的方法,   到底如何删除 cookie 
谢谢啦,

解决方案 »

  1.   

    把name的值设置为空 或者 立即过期
      

  2.   

    樓主參考一下:<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test</title>
    <style>
    *{ font-size:12px;}
    a{ color:#3399FF}
    a:hover{ color:##3399FF}
    a.hover{ color:#CC3300}
    </style>
    </head>
    <body>
    <div id="menu">
    <a href="javascript:void(0)" class="hover" onclick="changename(0)">我是第一</a>
    <a href="javascript:void(0)" onclick="changename(1)">我是第二</a>
    <a href="javascript:void(0)" onclick="changename(2)">我是第三</a>
    <a href="javascript:void(0)" onclick="changename(3)">我是第四</a>
    </div>
    <a href="javascript:clear();">点我清除cookie</a>
    <script language="javascript">
    function changename(c,cl)
    {
    var d=document.getElementById("menu").getElementsByTagName("a");
    if(!cl)
    {
    writeCookie("hovers",c,222);
    }
    c=readCookie("hovers")?readCookie("hovers"):c;
    for(i=0;i<d.length;i++)
    {
    d[i].className=i==c?"hover":"";
    }
    }
    function writeCookie(name, value, hours)
    {
      var expire = "";
      if(hours != null)
      {
        expire = new Date((new Date()).getTime() + 60 * 60 *1000);//+ hours
        expire = "; expires=" + expire.toGMTString();
      }
      document.cookie = name + "=" + escape(value) + expire;
    }
    // Example:
    // alert( readCookie("myCookie") );
    function readCookie(name)
    {
      var cookieValue = "";
      var search = name + "=";
      if(document.cookie.length > 0)
      { 
        offset = document.cookie.indexOf(search);
        if (offset != -1)
        { 
          offset += search.length;
          end = document.cookie.indexOf(";", offset);
          if (end == -1) end = document.cookie.length;
          cookieValue = unescape(document.cookie.substring(offset, end))
        }
      }
      return cookieValue;
    }
    function clear()
    {
    writeCookie("hovers","",222);
    document.location=document.location.href;
    }
    changename(0,1)
    </script>
    </body>
    </html>
      

  3.   

    是当我删除一个用户 这个用户的 cookie就要被清除 我要是设置为空 的话不管删除那个 那个都设置为空 不行的把
      

  4.   

    未经测试 你试试吧//添加cookie
    function addCookie(objName,objValue,objHours){
       var str = objName + "=" + escape(objValue);
       if(objHours > 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objHours*3600*1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
       }
       document.cookie = str;
       alert("添加cookie成功");
    }
    //获取指定名称的cookie的值
    function getCookie(objName){
       var arrStr = document.cookie.split("; ");
       for(var i = 0;i < arrStr.length;i ++){
        var temp = arrStr[i].split("=");
        if(temp[0] == objName) return unescape(temp[1]);
       } 
    }
    //为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
    function delCookie(name){
       var date = new Date();
       date.setTime(date.getTime() - 10000);
       document.cookie = name + "=a; expires=" + date.toGMTString();
    }
      

  5.   

    不用试了千篇一律的 网上copy 删除cookie方法跟我在网上找到的一样唉没人删除过cookie吗 没人遇到过吗 拍拖各位大牛
      

  6.   

    用jquery写的,你试试其实删除cookie就是把name的值设置为空,或过期时间设置为过去的某个时间
    (function($) {
    $.cookie = {
    enabled: navigator.cookieEnabled,
    getMap: function() {
    var _cookieItems = document.cookie.split(';');
    var _map = {};
    if(_cookieItems.length && $.trim(_cookieItems[0]) != '') {
    $.each(_cookieItems, function(i, v) {
    var _item = v.split('=');
    _map[decodeURI($.trim(_item[0]))] = decodeURI($.trim(_item[1]));
    });
    }
    return _map;
    },
    get: function(key) {
    var _map = this.getMap();
    return _map[key]? _map[key] : null;
    },
    /**
    * op = {
    * key: 'name',
    * path: 'storage path',
    * domain: 'domain',
    * }
    **/
    del: function(op) {
    var _v = this.get(key);
    if(_v != null) {
    this.set($.extend({
    value: '',
    time: -1
    }, op));
    }
    },
    /**
    * op = {
    * key: 'name',
    * value: 'value',
    * time: 'expires' units is seconds
    * path: 'storage path',
    * domain: 'domain',
    * len: how many values the name can have
    * flag: ',' if the name have some values, then use the flag to join them
    * }
    **/
    set: function(op) {
    if(op.key && typeof(op.value) != 'undefined' && op.value != null) {
    var _cookie = [];
    var _v = this.get(op.key);
    var _flag = op.flag? op.flag : '|';
    var _len = op.len? op.len : 1;
    if(_len > 1 && _v) {
    _v = _v.split(_flag);
    if(_v.length >= _len) {
    _v.splice(_len -1, _v.length - _len + 1);
    }
    op.value = op.value + _flag + _v.join(_flag);  
    }
    _cookie.push(op.key + '=' + encodeURI(op.value));
    if (op.time) {
    var _d = new Date();
    _d.setTime(_d.getTime() + (op.time * 1000));
    _cookie.push('expires=' + _d.toGMTString());
    }
    if (op.domain) {
    _cookie.push('domain=' + op.domain);
    }
    if (op.path) {
    _cookie.push('path=' + op.path);
    }
    document.cookie = _cookie.join(';');
    }
    }
    };
    })(jQuery);