RT,想在不关闭浏览器的情况下删除cookie,可是网上找了好多方法没有一个好使的,有没有大神指教一下啊?谢谢啦JavaScriptCookie

解决方案 »

  1.   

    你可以使用代码删除
    delCookie
    http://www.cnblogs.com/cuihongyu3503319/archive/2008/04/18/1160084.html
      

  2.   

    另外你要了解:删除Cookie只是设置cookie的过期时间,不是真的删除
      

  3.   

    删除cookie现成代码很多吧。。
    为什么会没有效果?function delCookie(name)
    {
    document.cookie = name + "=0;expires=" + 
    (new Date(((new Date()).getTime() - 1))).toGMTString();
    }比如这个简单的删除cookie,应该只要设置过期时间早于当前时间不就可以了?
    上面的代码IE9,Maxthon4,Chrome27均测试有效啊。
    至于浏览器删不删那不是你的事吧,它不删那也没得办法吧。。
      

  4.   

    JQUERY方法:
    //example $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});
    jQuery.cookie = function(name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }

            var path = options.path ? '; path=' + options.path : '';
            var domain = options.domain ? '; domain=' + options.domain : '';
            var secure = options.secure ? '; secure' : '';

            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }

            }

            return cookieValue;
        }
    }
      

  5.   

    补充一下
    $.cookie(’name’, null)就是删除COOKIE