删除现有Cookie

时间:2015-07-08 17:41:14

标签: javascript cookies

我使用以下JavaScript在网站上创建一个弹出窗口,并且只显示一次。现在,我的客户想要一个新的促销活动,我正在尝试删除现有的cookie并再次弹出它(以便已经访问过该网站的人再次看到弹出窗口,只需像以前一样)。这是当前的代码:

<!--
function setCookie(name, value, expires, path, domain, secure) {
 var curCookie = name + "=" + escape(value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
 document.cookie = curCookie;
}

function setCookie(name, value, expires, path, domain, secure) {
 var curCookie = name + "=" + escape(value) +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "") +
    ((expires) ? "; expires=" + expires.toGMTString() : "") ;
 document.cookie = curCookie;
}


function getCookie(name) {
 var dc = document.cookie;
 var prefix = name + "=";
 var begin = dc.indexOf("; " + prefix);
 if (begin == -1) {
  begin = dc.indexOf(prefix);
  if (begin != 0) return null;
 } else
  begin += 2;
 var end = document.cookie.indexOf(";", begin);
 if (end == -1)
  end = dc.length;
 return unescape(dc.substring(begin + prefix.length, end));
}
function pop()
{
 $(document).ready(function() {
    $('#myModal').reveal();
});
}
var seen = getCookie("seen");
if (!seen) {
var now = new Date();
now.setTime(now.getTime() + 360000 * 1000);
setCookie("seen", 1, now);
pop();
}
//-->

我尝试了以下方法来重置Cookie

<!--
function setCookie(name, value, expires, path, domain, secure) {
 var curCookie = name + "=" + escape(value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
 document.cookie = curCookie;
}

function setCookie(name, value, expires, path, domain, secure) {
 var curCookie = name + "=" + escape(value) +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "") +
   **";expires=Thu, 01 Jan 1970 00:00:01 GMT";**
 document.cookie = curCookie;
}


function getCookie(name) {
 var dc = document.cookie;
 var prefix = name + "=";
 var begin = dc.indexOf("; " + prefix);
 if (begin == -1) {
  begin = dc.indexOf(prefix);
  if (begin != 0) return null;
 } else
  begin += 2;
 var end = document.cookie.indexOf(";", begin);
 if (end == -1)
  end = dc.length;
 return unescape(dc.substring(begin + prefix.length, end));
}
function pop()
{
 $(document).ready(function() {
    $('#myModal').reveal();
});
}
var seen = getCookie("seen");
if (!seen) {
var now = new Date();
now.setTime(now.getTime() + 1 * 1000);
setCookie("seen", 1, now);
pop();
}
//-->

它不起作用。我是JavaScript的新手,非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我看到你有一个很好的混乱所以,如果我理解得很好,这个代码应该做的工作:

// on document ready
$(function(){

    // check for the old cookie and delete it
    if( Cookies.Check('seen') ) Cookies.Set('seen', '', -1); // delete the cookie if it exists

    // now work with a new one with other name
    if( !Cookies.Check('newmodal') ){ // if the cookie doesn't exist we show the modal and set the cookie
        $('#myModal').reveal();
        Cookies.Set('newmodal', 'true', 365); // days, if you need to use minutes see the method below
    } // there is no `else` here, if the cookie exists nothing happens
});



/**
 * Object with methods to manage cookies
 * @type Object
 */
var Cookies = {

    /**
     * Checks if a cookie exists
     * @param {String} name
     * @return Boolean
     */
    Check: function (name) {
        return !!this.Get(name);
    },

    /**
     * Gets a cookie value or returns false
     * @param {String} name
     * @return String|Boolean
     */
    Get: function (name) {
        var n, ca, c;
        n = name + "=";
        ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            c = ca[i].trim();
            if (c.indexOf(name) === 0) return c.substring(name.length + 1, c.length);
        }
        return false;
    },

    /**
     * Sets a cookie
     * @param {String} name
     * @param {String} value
     * @param {Number} [expire]
     * @param {Object} [options]
     * @return Boolean|void
     */
    Set: function (name, value, expire, options) {
        var d = new Date(), expires;
        var defaults = { expire_in: 'days', path: '/' };
        if (typeof options !== "undefined") $.extend(true, defaults, options);
        if (expire !== undefined && expire !== null) {
            if (defaults.expire_in == 'days') d.setDate(d.getDate() + expire);
            else if (defaults.expire_in == 'minutes') d.setDate(d.getTime() + expire * 1000);
            else {
                throw new JUtils.EX('expire_in configuration is not valid');
            }
            expires = "expires=" + d.toGMTString();
        }
        else expires = expires = "";
        document.cookie = name + "=" + value + "; " + expires + '; path=' + defaults.path;
        return true;
    }

};