开发者

how to get intitial triggering of window.onBind on page load in Chrome like in Firefox

i have that problem: i need to have a variable set to false/true depending on whether the page is loaded in the current tab or in an inactive tab. so i tried to do it with the focus-event, more or less like this (it'开发者_StackOverflow社区s jquery):

var hasFocus = false;    
$(function() {
    $(window).focus(function() {
        hasFocus = true;
    });
});

firefox and ie it do what i want: if the page is loaded in the active tab the event is triggered immediately, loaded in a background tab the event is only triggered when the tab gets active.

in chrome however the event does not get triggered when the page is loaded in the current active tab. does anybody know a workaround for this? i also tried events like mouseenter, hover but unfortunately they get executed on pageload in an inactive tab too... thanks in advance!


A tricky way would be this.

setInterval/setTimeout is only fired once a second at most for inactive tabs in Chrome. So, you could set an interval (or timeout) to be run after e.g. 10ms. If it only runs after a much longer time (e.g. 1 second), the page must be inactive. Otherwise, it would be run in 10ms (like you set).


I woulds suggest that you try mousemove as an event -- e.g.

var humanHasInteracted = false;    
$(function() {
    $(window).mousemove(function() {
        humanHasInteracted = true;
    });
});

alternatively use bind/unbind so that the event handler can removed when the first mousemovement is detected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜