弹出窗口无法通过javascript cookie工作-更新的代码

时间:2018-06-20 19:13:27

标签: javascript cookies

更新-我有一个使用javascript cookie(不允许使用jquery)弹出的页面,它将在3次访问,20%的时间且每6个月一次之后触发弹出窗口。我不知道为什么它不起作用。请,请帮助。我是javascript的初学者。 (我已更改网址以进行识别)

//setting the expiration date to 6 months from now//
var expDate = new Date();
// in the following line, 180 means 180 days.// 
expDate.setDate(expDate.getDate() + 180);
//making the cookie//
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

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 += 1;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie("CustomSurvey");
if (visits) {
    visits = 1;
}
if (visits > 4)
//Math.random is set so that 20% of those visits actually get the popup//
    if ((Math.random() * (100 - 1) + 1) < 20) {
      //get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
       var currentLocation = window.location;
        window.open("https://www.surveymonkey.com", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
    }
if (visits < 3) {
    ++visits;
    cookieData = visits;
    setCookie("CustomSurvey", cookieData, expDate);
}

2 个答案:

答案 0 :(得分:0)

您的代码中有一个错误,您缺少底部的双引号

window.open("surveymonkey.com, "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400"); 

应为:

window.open("surveymonkey.com", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400"); 

答案 1 :(得分:0)

浏览器不允许您打开弹出窗口,除非是响应用户触发的事件(例如点击)。

太多的网站触发广告弹出窗口(或弹出窗口,其中一些又触发了自己的弹出窗口)使用户需要保护。