2016-04-27 52 views
0

我创建一个应用程序,当其他人正在窗口将从一台计算机相互通信。例如,假设您正在与某人在Facebook上聊天,另一个人更改选项卡,或alt +选项卡或其他人 - 与另一个人会知道的(不是特定的,只是facebook窗口不在顶部并处于活动状态)聊天。的onblur和onfocus事件,但使用I帧

这是,最终,易:

/** 
* Functions which keeps track of whether the page is the active tab 
* this reports back to the host 
*/ 
window.onblur=function(){ 
    if(conn!=null) { 
     conn.send(JSON.stringify({'type':'upd','ele':'vis','status':0})); 
    } 
    make_noise('0000'); 
}; 
window.onfocus=function(){ // when you focus the window 
    if(conn != null) { 
     conn.send(JSON.stringify({'type':'upd','ele':'vis','status':1})); 
    } 
    stop_noise('0000'); 
}; 

这一切工作,没有任何问题。

但是我也有一个页面上的iframe中。当用户在iframe然后onblur事件被触发,它看起来像用户ALT +标签式,去了另一个标签等

我尝试这样解决:

window.onblur=function(){ // when you leave the window 
    if(conn != null && (document.activeElement != document.getElementById("content_frame"))) {  
      conn.send(JSON.stringify({'type':'upd','ele':'vis','status':0})); 
     } 
    } 
    make_noise('0000'); 
}; 

但是因为IFRAME保持活动元素,无论窗口当前是否处于活动状态,都会导致误报。

有人能想出解决的办法? $('iframe')。is(':focus')was was as a testing of a focus of the iframe,but then works like same as activeElement ...

非常感谢, Alex

+0

父母和IFrame都来自同一个来源? – 11thdimension

+0

你是什么意思? :) –

+0

看看这个https://en.wikipedia.org/wiki/Same-origin_policy,通常这意味着同一个域'协议://域名:port' – 11thdimension

回答

0

您可以检查这个样子。我在我的代码中使用jquery btw。

$(window).blur(function() { 
    if ($('iframe').is(':focus')) { 
    ... 
    } else { 
    ... 
    }     
}); 
+0

你也可以绑定和解除绑定事件以避免重复调用。 – kanchan

+0

感谢您对此的支持,但是我在原始帖子中说过,由于某种原因,这不起作用:window.onblur = function(){\t //当您离开窗口时 \t if(conn!= null){ \t \t如果($( 'IFRAME')为( ':聚焦')){\t \t \t \t \t \t conn.send(JSON.stringify({ '类型': 'UPD', 'ELE': '可见' , '状态':1})); \t \t}否则{ \t \t \t conn.send(JSON.stringify({ '类型': 'UPD', 'ELE': '可见', '状态':0})); \t \t} \t} }; –