admin管理员组文章数量:1287488
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 website.
Is there a way to do this with JavaScript / jQuery? I know that location.reload();
is used to refresh a page, but I don't know how to tell JavaScript to do this when the tab bees active again (and only once then).
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 website.
Is there a way to do this with JavaScript / jQuery? I know that location.reload();
is used to refresh a page, but I don't know how to tell JavaScript to do this when the tab bees active again (and only once then).
1 Answer
Reset to default 11You can use this:
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
Usage:
var visible = vis(); // gives current state
vis(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
本文标签: javascriptReload page when user returns from other tabStack Overflow
版权声明:本文标题:javascript - Reload page when user returns from other tab - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741311957a2371695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论