簡體   English   中英

檢測移動到Mobile Safari中的新選項卡

[英]Detect moving to a new tab in Mobile Safari

我有一系列打開彈出窗口的頁面(Mobile Safari中的新標簽。)這些彈出窗口中的每一個都需要知道它們何時成為焦點。 在桌面上,我們使用window.onblurwindow.onfocus來驅動此行為。 但是,這些事件都不適用於iPad。 我也嘗試了window.onpageshowwindow.onpagehide ,它們似乎也沒有在正確的時間點火。 我有一個測試HTML文件:

<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>

理論上,當您單擊“單擊我”時,您應該在出現新窗口時出現模糊事件。 但這不會發生在Mobile Safari上。 onpagehideonpageshow顯示沒有愛,它們只能幫助您檢測何時關閉標簽。

如何在Mobile 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,並且僅將焦點hack用作后備,選擇在可用的情況下使用Visibility API(理想情況下使其面向未來)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM