即使用户正在导航到其他页面,也要在一段时间后打开弹出窗口

Make popup opening after a while even if the user is navigating to other page

本文关键字:一段时间 窗口 用户 导航 其他      更新时间:2024-06-30

我需要一些关于这段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秒就会弹出。

提前感谢

这里要克服的根本问题是在页面之间传递"state"。由于您已经在示例中使用了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秒传达一次更新后的值。

没有办法。一旦卸载页面,您就不能做任何其他事情。您需要使用服务器上的其他资源(Cookie、会话等)来检查窗口是否已显示。

但是使用document.cookie是不可能的。

您需要使用服务器端cookie或HTML本地存储/会话存储。

类似Response.SetCookie("Visited", true)。我不知道你在用什么作为后台。