检查是否windows / page / document / iframe具有焦点 [英] Check if windows/page/document/iframe has focus

查看:160
本文介绍了检查是否windows / page / document / iframe具有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提出了类似的问题,但我需要一个更具体的案例,所以我决定提出一个新的问题,不要混淆。



检查窗口是否有焦点



1。我正在建立一个PTC网站,用户通过访问网站获得付款x秒。查看这样的工作:打开一个新的页面,包含两个东西,顶部的酒吧与计数器和iframe,其中包含广告客户的网站)。
3。我想要强制用户查看广告客户支付的x秒的页面。

4。要检查用户是否正在查看页面IMO,我需要检查文档或iframe有焦点。



问题:

1。因为用户可以使用 window.onblur window.onfocus 在页面载入前绕过这些命令。

2。我不能使用 hasFocus() iframe,因为Chrome会抛出这个错误:阻止一个来源为xxx的框架访问一个源于yyy的框架。协议,域和端口必须匹配。



我知道有两个类似的网站可以实现这个功能,但是他们的javascript非常奇怪,不要理解单行。 (neobux.com和clixsense.com)



我疯了,因为我已经尝试了很多东西,但都没有成功,我真的需要帮助this。
Thanks!

解决方案

  var has_focus = true; 

函数loading_time(){
$(:focus)。each(function(){
if($(this).attr(id)== iframeID)has_focus = true;
});

if(has_focus == true)alert('page have focus');
else alert('page has not focus');

setTimeout(loading_time(),2000);


window.onblur = function(){
has_focus = false;
}
window.onfocus = function(){
has_focus = true;

$ b $(window).load(function(){
setTimeout(loading_time(),2000);
});

为了使效率更高,您需要 var has_focus = false; 并让用户点击页面上的某处。


I've asked a similar question already, but I need it for a more specific case so I decided to make a new question to don't mix things up.

Check if window has focus

1. I'm making a PTC site, where users get paid by visiting sites for x seconds.
2. The ad view works like this: A new page is opened, containing two things, the top bar with the counter and the iframe which contains the advertiser's site).
3. I want to force the user to see that page for the x seconds that the advertiser paid for.
4. To check if the user is seeing the page, IMO, I need to check if either the document or iframe has focus.

Problems:
1. I can't use window.onblur or window.onfocus since the user can lose focus before the page loads and bypass those commands.
2. I can't use hasFocus() on the iframe because Chrome throws this error: Blocked a frame with origin "xxx" from accessing a frame with origin "yyy". Protocols, domains, and ports must match.

I know two similar sites that accomplish this, but their javascript is so weird that I can't understand a single line. (neobux.com and clixsense.com)

I'm going crazy on this because I've tried so many things and none of them worked, I really need help on this.
Thanks!

解决方案

var has_focus = true;

function loading_time() {
    $(":focus").each(function() {
      if($(this).attr("id")=="iframeID") has_focus = true;
    });

    if(has_focus==true) alert('page has focus');
    else alert('page has not focus');

    setTimeout("loading_time()", 2000);
}

window.onblur = function(){  
    has_focus=false;  
}  
window.onfocus = function(){  
    has_focus=true;  
}

$(window).load(function(){
    setTimeout("loading_time()", 2000);
});

To make it more efficient, you need to var has_focus = false; and make the user click somewhere on the page.

这篇关于检查是否windows / page / document / iframe具有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆