how to check if browser window has focus in “mobile web browser”

老子叫甜甜 提交于 2019-12-11 08:40:56

问题


I am not able to test this event on chrome mobile web browser, https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus

I would like to test application, such that user clicks on home page of mobile phone and then I want to take certain action in my application.

Note : Basically on web browser I am able to achieve this functionality using document.hasfocus, window.blur but not on mobile web browser

For eg. http://jsfiddle.net/QkzvP/

$(function(){

    window['hasFocus'] = false;

    $(window)
        .bind('focus', function(ev){
            window.hasFocus = true;
            $('#event').append('<div>'+(new Date()).getTime()+' focus</div>');
        })
        .bind('blur', function(ev){
            window.hasFocus = false;
            $('#event').append('<div>'+(new Date()).getTime()+' blur</div>');
        })
        .trigger('focus');

    setInterval(function() {
        $('#event').append('<div>'+(new Date()).getTime()+' has focus '+(window.hasFocus ? 'yes' : 'no')+'</div>');
    }, 1000);
});

OR http://jsfiddle.net/Msjyv/3/

function check()
{
    if(document.hasFocus() == lastFocusStatus) return;

    lastFocusStatus = !lastFocusStatus;
    statusEl.innerText = lastFocusStatus ? 'with' : 'without';
}

window.statusEl = document.getElementById('status');
window.lastFocusStatus = document.hasFocus();

check();
setInterval(check, 200);

来源:https://stackoverflow.com/questions/42470145/how-to-check-if-browser-window-has-focus-in-mobile-web-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!