Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除现有cookie_Javascript_Cookies - Fatal编程技术网

Javascript 删除现有cookie

Javascript 删除现有cookie,javascript,cookies,Javascript,Cookies,我使用下面的JavaScript在网站上创建了一个弹出窗口,它只显示一次。现在,我的客户想要一个新的促销活动,我正在尝试删除现有的cookie并使其再次弹出(这样,已经访问过该网站的人可以再次看到弹出窗口,只像以前一样出现一次)。以下是当前代码: <!-- function setCookie(name, value, expires, path, domain, secure) { var curCookie = name + "=" + escape(value) + ((ex

我使用下面的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();
}
//-->

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

<!--
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新手,非常感谢您的帮助

我看到您的情况很糟糕,因此,如果我理解得很好,以下代码应该可以完成这项工作:

// 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;
    }

};
//已准备好文档
$(函数(){
//检查旧cookie并将其删除
if(Cookies.Check('seed'))Cookies.Set('seed',''-1);//如果cookie存在,则删除它
//现在使用另一个名称的新名称
如果(!Cookies.Check('newmodal'){//如果cookie不存在,则显示模式并设置cookie
$('#myModal').reveal();
Cookies.Set('newmodal','true',365);//天,如果需要使用分钟,请参阅下面的方法
}//这里没有'else',如果cookie存在,则不会发生任何事情
});
/**
*对象,该对象具有管理Cookie的方法
*@type对象
*/
变量Cookies={
/**
*检查是否存在cookie
*@param{String}name
*@返回布尔值
*/
检查:功能(名称){
return!!this.Get(name);
},
/**
*获取cookie值或返回false
*@param{String}name
*@返回字符串|布尔值
*/
Get:函数(名称){
var n,ca,c;
n=名称+“=”;
ca=document.cookie.split(“;”);
对于(变量i=0;i
我需要删除现有的窗口,以便已经访问过网站的用户再次看到弹出窗口,然后再次保存,这样它就不会每次都弹出。请尝试改用
localStorage
?现在有了坚实的支持,概念上更容易,而且更容易找出错误的方向。kmsdev,感谢您的帮助!如果我通过浏览器手动清除现有Cookie,它会起作用,但代码本身不会清除现有Cookie。我的问题是Cookie以前保存过,所以以前访问过该网站的每个人都看不到新的弹出窗口。我需要重新设置,这样他们才能再次看到新的弹出窗口。不客气。您只需删除旧cookie,然后使用其他名称设置新cookie。我更新了我的答案。我很高兴。如果解决了问题,请检查答案是否有效