简体   繁体   English

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

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

I need a bit of help with this javascript code. 我需要一些关于这个javascript代码的帮助。

I have this code: 我有这个代码:

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

It's supposed to open a popup after 30 seconds, one time per day. 它应该在30秒后打开一个弹出窗口,每天一次。 The issue is that the popup it's opening after 30 seconds on stay on a page. 问题是弹出窗口在停留在页面上30秒后打开。 It's there any way to make it to open after 30 seconds even if the client navigate to other page ? 即使客户端导航到其他页面,它有没有办法让它在30秒后打开? So, if the user stay 15 seconds on a page and 15 on another, getting the popup. 因此,如果用户在页面上停留15秒而在另一页上停留15秒,则获取弹出窗口。

Thank you in advance 先感谢您

Yes. 是。

But it's not possible using document.cookie . 但是使用document.cookie是不可能的。

You would need to use server-side cookies or HTML local storage/session storage. 您需要使用服务器端cookie或HTML本地存储/会话存储。

Something like, Response.SetCookie("Visited", true) . Response.SetCookie("Visited", true) I don't know what you're using as back end. 我不知道你用什么作为后端。

No way man. 没门。 Once the page is unloaded you cannot do anything else. 卸载页面后,您无法执行任何其他操作。 You need to use other resources on server (cookies, session, etc..) to check if the window is already displayed or not. 您需要使用服务器上的其他资源(cookie,会话等)来检查窗口是否已经显示。

The fundamental issue to overcome here is passing the 'state' between pages. 这里需要克服的根本问题是在页面之间传递“状态”。 Since you're already using cookies in your example, we'll work with that. 由于您已经在示例中使用了Cookie,因此我们将使用它。 You need to set a session cookie with the time the user has been on the site (initially 0). 您需要设置一个会话cookie,其中包含用户访问该站点的时间(最初为0)。 You'd then need to 'poll' the cookie once every so often (every 5 seconds maybe) to update the total time on site count, and read it back. 然后,您需要每隔一段时间(可能每5秒)“轮询”一次cookie,以更新网站计数的总时间,并将其读回。 If it's 30 seconds or more, fire the popup. 如果是30秒或更长时间,则触发弹出窗口。

So instead of using: 所以不要使用:

setTimeout(function() {
    // open alert box
}, 30000);

You'd do something like: 你会做类似的事情:

setTimeout(function() {
    // Increment 'time-on-site' cookie value by 5000
    // Then, if 'time-on-site' cookie value >= 30000, fire popup
}, 5000);

UPDATE Of course, this requires a lot more back-and-forth to the server, as you need to communicate the updated value every 5 seconds. 更新当然,这需要更多来回服务器,因为您需要每5秒传递一次更新的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航到其他页面时,SweetAlert - SweetAlert while navigating to other page 即使在弹出窗口中导航到新页面时,也要保持指向回调的指针 - Keep a pointer to a callback even when navigating to a new page in a popup window 在导航到新页面时保持 Chrome 扩展弹出窗口打开 - Keep Chrome extension popup open while navigating to a new page 如何检查用户是否正在刷新页面或导航到其他页面 - How to check if user is refreshing the page or navigating to other page 防止在打开引导程序模式弹出窗口时加载其他类 - Preventing loading other classes while opening bootstrap modal popup 如何在导航/重新加载到其他页面时停止jquery功能? - How to stop jquery function while navigating/reloading to other page? 为什么某些网页可以直接打开弹出窗口,而另一些网页则需要用户确认才能打开? (谷歌浏览器) - Why some web-page can open popup directly while others need the user to confirm before opening? (Google Chrome) 导航到其他页面后如何保留文本框值? - How to retain textbox values after navigating to other page? 在页面底部打开弹出窗口 - Popup opening at bottom of page 打开新页面后如何将焦点设置在弹出窗口上? - How to set focus on popup after opening a new page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM