【发布时间】:2013-10-31 10:07:16
【问题描述】:
如何检测用户是否正在切换到另一个浏览器选项卡?
目前,我有这个:
$(window).on("blur focus", function (e) {
var prevType = $(this).data("prevType");
if (prevType != e.type) { // reduce double fire issues
switch (e.type) {
case "blur":
$('.message').html('<div class="alert alert-error">Oops. You navigated away from the ads <a id="start" class="butt green">Resume</a></div>');
var myDiv = $("#bar");
myDiv.clearQueue();
myDiv.stop();
clearInterval($timer);
$timer = null;
break;
case "focus":
// do work
break;
}
}
$(this).data("prevType", e.type);
});
但这仅在用户最小化活动窗口时才有效。
【问题讨论】:
-
在我看来,那些问答已经过时了。可见性 API 现在可以提供更好的答案。
-
我同意dystroy。他们都是四岁以上;从那时起,网络发生了很大变化。但是,oliverbj,您最好将这些作为使用旧浏览器的人的参考。
-
@MattGrande 也许更新后的答案也应该在那里发布,那么?
-
@JanDvorak 我在两个问题上都写了两个 cmets。
标签: javascript jquery