【问题标题】:Remove Cookies In Jquery on logout button在注销按钮上删除 Jquery 中的 Cookie
【发布时间】:2014-10-04 14:12:48
【问题描述】:

您好,我正在尝试在 jquery onclick of logout button 中为清除 cookie 编写代码,但没有得到解决方案

 function logout()
{
 document.cookie = 'Visit=; expires='+new Date(0).toUTCString() +'; path=/FinalVertozz/';
 window.location='../login.html'; 

}

cookie 详情

名称:访问 内容:09850227123455130 域:本地主机 路径:/FinalVertozz 发送:任何类型的连接 可访问脚本:是 创建时间:2014 年 10 月 4 日,星期六 11:25:45 Expires:浏览会话结束时

【问题讨论】:

标签: javascript jquery cookies


【解决方案1】:

你能用简单的javascript吗?这很容易。

function ClearCookies()
{
   var cookiesCollection = document.cookie.split(";");
   for (var i = 0; i < cookiesCollection .length; i++) 
    {
    var cookieName = cookiesCollection [i];
    var pos= cookieName.indexOf("=");
    var name = pos> -1 ? cookieName.substr(0, pos) : cookieName;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

【讨论】:

    【解决方案2】:

    来自@Russ Cam 的这个问题和答案的参考 How do I set/unset cookie with jQuery? 使用这个函数复用

    function createCookie(name, value, days) {
        var expires;
    
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        } else {
            expires = "";
        }
        document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
    }
    
    function readCookie(name) {
        var nameEQ = escape(name) + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) === ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
        }
        return null;
    }
    
    function eraseCookie(name) {
        createCookie(name, "", -1);
    }
    

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      这个问题是路径。如果你删除它,一切都会奏效。

      document.cookie = 'Visit=; expires='+new Date(0).toUTCString();
      

      另外,您在上面的示例中没有使用任何 jQuery。这个问题可以更好地表述为“在退出按钮时删除 JavaScript 中的 cookie”。

      【讨论】:

        猜你喜欢
        • 2012-10-01
        • 2022-11-21
        • 1970-01-01
        • 2015-03-12
        • 1970-01-01
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 2013-10-26
        相关资源
        最近更新 更多