Skip to content Skip to sidebar Skip to footer

Reload Page When User Returns From Other Tab

I work on some kind of website that places importance on being up-to-date. For that purpose, I need to refresh the page when the user switches from another tab to the tab with the

Solution 1:

You can use this:

var vis = (function(){
var stateKey, eventKey, keys = {
    hidden: "visibilitychange",
    webkitHidden: "webkitvisibilitychange",
    mozHidden: "mozvisibilitychange",
    msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
    if (stateKey indocument) {
        eventKey = keys[stateKey];
        break;
    }
}
returnfunction(c) {
    if (c) document.addEventListener(eventKey, c);
    return !document[stateKey];
}
})();

Usage:

var visible = vis(); // gives current statevis(aFunction);      // registers a handler for visibility changes`vis(function(){
    document.title = vis() ? 'Visible' : 'Not visible';
});

You can readabout this here:

Detect if browser tab is active or user has switched away

Post a Comment for "Reload Page When User Returns From Other Tab"