如何检测页面或选项卡是否为当前活动页面 [英] How to detect if page or tab is the current active page

查看:60
本文介绍了如何检测页面或选项卡是否为当前活动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...我想问你们中是否有人知道如何检测您当前的页面是否为选中/活动页面?您看到我有一个脚本来检测您的页面是否现在已为选定的选项卡...但是我想要一个代码,以便在页面加载完毕后该脚本将检查该页面是否为当前选定的页面...这是我的代码检查您的页面是否为选定的选项卡...

Hello guys... I would like to ask if anyone of you knows how to detect if your current page is the selected/active one? you see i have a script that detects if your page is now the selected tab... but i want a code that when the page finish loading the script will check if the page is the currently selected page... here is my code that check if your page is the selected tab...

window.onfocus = function () {
    isActive = true;
 };

window.onblur = function () {
    isActive = false;
 };



因此,我想在页面加载完成后自动检测当前页面是否处于活动状态...我该怎么做?根据我在一个网站上复制的代码进行搜索,该代码仅在您从一种浏览器切换到另一种浏览器或从一个标签页切换到另一种标签页时起作用,现在我的问题是如果我的页面加载完成怎么办我检测到它是否是当前选择的标签吗?

我已将变量isActive设置为在首次加载时为true,并且在如果用户停留在该页面上"可以正常工作.但是,当用户刷新页面并在页面加载之前切换到另一个选项卡时,就会出现问题,即使用户没有查看该页面,我的变量isActive也会自动设置为true ...有人知道可以解决此问题吗?我已经看到了此链接,但不是我在寻找的链接 http://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active



So i want when my page finish loading it will automatically detect if its the active page or not... how do i do this? base from this code which i copied on one of the site i did some searching... this code only works when you switch from one browser to another or switch from one tab page to another now my problem is if my page finish loading how do i detect if its the current selected tab or not?

I have set the variable isActive to be true on first load and it works fine "if the user stayed on that page". but the problem arises when the user refresh the page and switch to another tab before the page loaded then my variable isActive is automatically set to true even though the user is not viewing that page... anyone one knows a work around for this one? i already saw this link but its not what i was looking for http://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active

推荐答案

我认为您应该做一些奇怪的事情.
例如,一起使用onfocus和onmousemove.
调用onmosemove时,您可以100%确保该页面处于活动状态
因此请不要在onFocus上设置isActive = true,而应在onMousemove函数上将其替换.
此致Jamal.
In my opinion you should do some thing strange.
for instance use onfocus and onmousemove together.
when onmosemove is called you are 100% sure that page is active
so do not set isActive=true on onFocus, replace it on onMousemove function.
best regards Jamal.


这篇关于如何检测页面或选项卡是否为当前活动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆