Detect moving to a new tab in Mobile Safari

后端 未结 4 622
粉色の甜心
粉色の甜心 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: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);
    

提交回复
热议问题