[英]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.