[英]Make popup opening after a while even if the user is navigating to other page
我需要一些关于这个javascript代码的帮助。
我有这个代码:
jQuery(document).ready(function(){
if (document.cookie.indexOf('visited=true') == -1) {
var fifteenDays = 1000*60*60*24*1;
var expires = new Date((new Date()).valueOf() + fifteenDays);
document.cookie = "visited=true;expires=" + expires.toUTCString();
window.setTimeout(
function() {
jQuery.colorbox({href:"/popup.htm", open:true, iframe:false, innerWidth:600, innerHeight:490});
},
30000 )}});
它应该在30秒后打开一个弹出窗口,每天一次。 问题是弹出窗口在停留在页面上30秒后打开。 即使客户端导航到其他页面,它有没有办法让它在30秒后打开? 因此,如果用户在页面上停留15秒而在另一页上停留15秒,则获取弹出窗口。
先感谢您
是。
但是使用document.cookie
是不可能的。
您需要使用服务器端cookie或HTML本地存储/会话存储。
像Response.SetCookie("Visited", true)
。 我不知道你用什么作为后端。
没门。 卸载页面后,您无法执行任何其他操作。 您需要使用服务器上的其他资源(cookie,会话等)来检查窗口是否已经显示。
这里需要克服的根本问题是在页面之间传递“状态”。 由于您已经在示例中使用了Cookie,因此我们将使用它。 您需要设置一个会话cookie,其中包含用户访问该站点的时间(最初为0)。 然后,您需要每隔一段时间(可能每5秒)“轮询”一次cookie,以更新网站计数的总时间,并将其读回。 如果是30秒或更长时间,则触发弹出窗口。
所以不要使用:
setTimeout(function() {
// open alert box
}, 30000);
你会做类似的事情:
setTimeout(function() {
// Increment 'time-on-site' cookie value by 5000
// Then, if 'time-on-site' cookie value >= 30000, fire popup
}, 5000);
更新当然,这需要更多来回服务器,因为您需要每5秒传递一次更新的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.