【发布时间】:2021-12-25 17:04:30
【问题描述】:
我想在网站上显示弹窗,场景是这样的。
- 如果用户访问 5 个页面显示弹出窗口
- 如果用户停留在网站上并花费 2 分钟显示弹出窗口
- 如果用户关闭弹窗,下次就不会再出现了。
工作正常,但我不知道如何解决问题 2(如果用户停留在网站上并花 2 分钟显示弹出窗口)
我尝试使用本地存储,但没有帮助。
这里是代码...
var popup = '<div class="popup-cover" id="sub-popup">';
popup += '<div id="subscribe-popup" class="popup">';
popup += '<a class="close" onclick="closePopup()">×</a>';
popup += '<h1 class="sub-title">Subscribe here!</h1>';
popup += '<p class="sub-txt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>'
popup += '<a class="btn sub-btn btn-nature" onclick="hidePopup()">Subscribe</a>';
popup += '</div>';
popup += '</div>';
function closePopup() {
$('#sub-popup').remove();
localStorage.displayPopup = 0;
}
function hidePopup() {
$('#sub-popup').remove();
localStorage.displayPopup = 0;
window.open("https://upesy.us17.list-manage.com/subscribe/post?u=4f67f99a5b6bf0f744449df73&id=45f50ab267", "_blank");
}
jQuery(document).ready(function($) {
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function displayPopup() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if (!(visit = GetCookie("upesy-cookie")))
visit = 0;
visit++;
SetCookie("upesy-cookie", visit, expdate, "/", null, false);
if (visit >= 4) {
if(localStorage.displayPopup != 0) {
setTimeout(function(){
$(document.body).append(popup);
SetCookie("upesy-cookie", 0);
}, 2000);
}
}
}
//window.onload = displayPopup
$(window).on("load", displayPopup);
});
【问题讨论】:
-
在页面加载时启动计时器并在 2 分钟后打开弹出窗口。
-
如果用户在网站的特定页面停留 2 分钟
-
我不明白。计时器为每一页重新启动。点击链接将重置计时器。
-
如果我们在 cookie 的帮助下这样做将是最好的选择,因为它会影响每个页面。如果用户在网站上停留 2 分钟而不是在特定页面上
-
首先你写了“如果用户在特定页面上停留2分钟”然后你写“如果用户在网站上停留2分钟不在特定页面上”。这个问题我不清楚。
标签: javascript jquery cookies