Detect moving to a new tab in Mobile Safari

后端 未结 4 547
粉色の甜心
粉色の甜心 2020-12-30 13:12

I have a series of pages that open popups (new tabs in Mobile Safari.) Each of these popups needs to know when they are the focused or not. On desktops, we use window.

相关标签:
4条回答
  • 2020-12-30 13:27

    Someone asked the same thing a little more recently so I'll just link this answer to my older one here.

    Mageek's method is very similar to what I'm doing, but will also fire on a scroll event or when the keyboard is visible. Preventing the behavior upon scrolling wasn't terribly challenging, but I never got around to looking up on-screen keyboard events.

    My object also leverages requestAnimationFrame and will use the focus hack only as a fallback, opting to use the Visibility API where available (ideally making it future-proof).

    0 讨论(0)
  • 2020-12-30 13:28

    I don't think that onblur can be detected, but this is a code to detect 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);
    

    Then you just write:

    document.addEventListener("focus",function()
    {
        alert("Focus detected");
    },false);
    
    0 讨论(0)
  • 2020-12-30 13:43

    Try this: https://gist.github.com/1122546

    It's a Visibilty API polyfill. Should do the trick.

    0 讨论(0)
  • 2020-12-30 13:43

    iOS 5 pause JS in none active tab. Maybe this topic could help you.

    ios 5 pauses javascript when tab is not active

    Handling standby on iPad using Javascript

    0 讨论(0)
提交回复
热议问题