首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

如何用js在不关闭浏览器的情况下立即删除cookie

2013-07-16 
怎么用js在不关闭浏览器的情况下立即删除cookieRT,想在不关闭浏览器的情况下删除cookie,可是网上找了好多

怎么用js在不关闭浏览器的情况下立即删除cookie
RT,想在不关闭浏览器的情况下删除cookie,可是网上找了好多方法没有一个好使的,有没有大神指教一下啊?谢谢啦 JavaScript Cookie
[解决办法]
你可以使用代码删除
delCookie
http://www.cnblogs.com/cuihongyu3503319/archive/2008/04/18/1160084.html
[解决办法]
删除cookie现成代码很多吧。。
为什么会没有效果?


function delCookie(name)
{
document.cookie = name + "=0;expires=" + 
(new Date(((new Date()).getTime() - 1))).toGMTString();
}

比如这个简单的删除cookie,应该只要设置过期时间早于当前时间不就可以了?
上面的代码IE9,Maxthon4,Chrome27均测试有效啊。
至于浏览器删不删那不是你的事吧,它不删那也没得办法吧。。
[解决办法]
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;
    }
}

热点排行