窗口/选项卡未激活时弹出警报

发布于 2024-11-04 15:03:48 字数 174 浏览 3 评论 0原文

我在这里尝试了很长一段时间但没有运气。

您知道如何在用户从浏览器中更改选项卡/窗口时显示弹出(警报)吗?

所以基本上,当用户在他/她的浏览器上更改窗口时,警报窗口将会弹出,用户必须按“确定”按钮才能继续,

因为我正在创建一个在线测试站点并且需要显示每当用户尝试更改选项卡/窗口时发出警报

I am trying for quite some time here but with no luck.

Do you have any idea how you can make a pop-up (alert) show, whenever the user changes tab/window from his browser?

So basically, when the user changes a window at his/her browser, the alert window will pop and the user will have to press the ok button to continue

This funcitonality is needed as I am creating an online tests site and I need to display the alert whenever the users will try to change tab/window

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-11-11 15:03:48

您可以在窗口对象上使用 focusblur 事件(选项卡也被视为窗口。)尝试一下:

window.addEventListener('focus', function() { console.log('Window has focus'); });
window.addEventListener('blur', function() { console.log('Window lost focus'); });

或使用 jQuery:

$(window).focus(function() { console.log('Window has focus'); });
$(window).blur(function() { console.log('Window lost focus'); });

另外,请小心使用警报以这种方式。许多用户可能会觉得有点烦人。

You can use the focus and blur events on the window object (tabs are considered windows, too.) Try this out:

window.addEventListener('focus', function() { console.log('Window has focus'); });
window.addEventListener('blur', function() { console.log('Window lost focus'); });

or with jQuery:

$(window).focus(function() { console.log('Window has focus'); });
$(window).blur(function() { console.log('Window lost focus'); });

Also, be careful about using alerts in this manner. Many users may find it a bit annoying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文