使用Javascript来检测用户是否更改选项卡 [英] Javascript to detect if user changes tab

查看:76
本文介绍了使用Javascript来检测用户是否更改选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想为在线测验编写一个网页。我的基本要求是,如果参加测验的人改变标签或打开新窗口,即使没有最小化浏览器,即如果该人试图从某些人看到答案其他窗口/选项卡,测验应该停止。我该怎么做?

PS:它不应该是一个非常新的HTML5功能。我希望它可以被所有主流浏览器支持。

解决方案

您可以通过将模糊/焦点事件侦听器附加到窗口来确定选项卡或窗口是否处于活动状态。 b

在jQuery中它将是

  $(window).focus(function(){
//做某事
});

$(window).blur(function(){
//做某事
});

引用此回答: https://stackoverflow.com/a/1760268/680578



I want to write a webpage for online quiz.Basic requirement i have is that, If the person taking quiz changes tab or opens new window even without minimizing browser, i.e if the person tries to see the answer from some other window/tab , quiz should stop.How can i do that?
P.S: it shouldn't be some very new HTML5 feature.I want it to be supported by all major browser currently.

解决方案

You can determine if a tab or window is active by attaching a blur / focus event listener to window.

in jQuery it would be

$(window).focus(function() {
    //do something
});

$(window).blur(function() {
    //do something
});

quoted from this SO answer: https://stackoverflow.com/a/1760268/680578

这篇关于使用Javascript来检测用户是否更改选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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