我创建一个应用程序,当其他人正在窗口将从一台计算机相互通信。例如,假设您正在与某人在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
父母和IFrame都来自同一个来源? – 11thdimension
你是什么意思? :) –
看看这个https://en.wikipedia.org/wiki/Same-origin_policy,通常这意味着同一个域'协议://域名:port' – 11thdimension