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