检测移动到移动Safari中的新标签

我有一系列打开popup窗口的页面(Mobile Safari中的新选项卡)。这些popup窗口中的每一个都需要知道它们是何时关注的。 在桌面上,我们使用window.onblurwindow.onfocus来驱动这种行为。 但是,这些事件都不能在iPad上使用。 我也尝试了window.onpageshowwindow.onpagehide ,似乎没有在正确的时间开火。 我有一个testingHTML文件:

 <html> <head> <script language="javascript"> console.log('Hello'); window.onblur = function(e) { console.log('blur'); }; window.onfocus = function(e) { console.log('focus'); }; window.onpagehide = function(e) { console.log('pagehide'); }; window.onpageshow = function(e) { console.log('pageshow'); }; </script> </head> <body> <a href="http://www.google.com" target="_blank">Click Me</a> </body> </html> 

理论上,当你点击“Click Me”时,当出现新窗口时,你应该得到一个模糊事件。 但是这在移动Safari上不会发生。 onpagehideonpageshow显示任何爱,他们只是帮助检测当你要closures标签。

我如何获得我在移动Safari浏览器中查找的行为? 有没有可能?

试试这个: https : //gist.github.com/1122546

这是一个Visibilty API polyfill。 应该做的伎俩。

我不认为可以检测到onblur ,但这是一个检测onfocus的代码:

 var timestamp=new Date().getTime(); function checkResume() { var current=new Date().getTime(); if(current-timestamp>5000) { var event=document.createEvent("Events"); event.initEvent("focus",true,true); document.dispatchEvent(event); } timestamp=current; } window.setInterval(checkResume,50); 

然后你只写:

 document.addEventListener("focus",function() { alert("Focus detected"); },false); 

iOS 5暂停JS在没有活动选项卡。 也许这个话题可以帮助你。

当选项卡未激活时,ios 5暂停JavaScript

使用Javascript处理iPad的待机状态

最近有人问了同样的事情,所以我只是把这个答案链接到我的旧版本。

Mageek的方法与我正在做的非常相似,但也会在滚动事件或键盘可见时触发。 防止滚动的行为并不是非常具有挑战性,但我从来没有去查找屏幕上的键盘事件。

我的对象还利用了requestAnimationFrame,并将焦点黑客作为后备,select使用可见性API(理想情况下,使其面向未来)。