Make sure the focus of the window / page / document / iframe

I already asked a similar question, but I need it for a more specific case, so I decided to ask a new question so as not to mix things up.

Check if the window has focus

1. I make a PTC website where users get paid for visiting sites in x seconds.
2 .. An ad looks like this: a new page opens containing two things: the top panel with a counter and an iframe that contains the advertiser's site).
3. I want to make the user see this page in x seconds for which the advertiser paid.
4. To check if the user sees the page, IMO, I need to check if the document or iframe has focus.

Problems:
1. I cannot use window.onblur or window.onfocus , as the user may lose focus before loading the page and bypass these commands. 2. I cannot use hasFocus() in an iframe because Chrome makes this error: Blocked a frame with origin "xxx" from accessing a frame with origin "yyy". Protocols, domains, and ports must match. 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 do this, but their javascript is so weird that I can't understand a single line. (neobux.com and clixsense.com)

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

+1
source share
1 answer
 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 var has_focus = false; and make the user click somewhere on the page.

+1
source

All Articles