繁体   English   中英

是否可以检测用户是否打开了新标签页?

[英]Is it possible to detect if a user has opened a new tab?

我们正在开发一个测验平台,我们希望在该平台上检测用户是否在测验中打开新标签页。 不允许用户打开新标签,我们希望跟踪该标签以通知管理测验的老师。 我们主要针对主要浏览器(IE,Chrome,Firefox,Safari)。

这可能吗? 谢谢

您可以使用window对象的onblur事件并编写如下内容:

function userCheated() {
    // The user cheated by leaving this window (e.g opening another window)
    // Do something
    alert("You can't cheat!");
}

window.onblur = userCheated;

注意:由于当用户离开您的窗口时,您无法再对该窗口进行控制,由于用户执行缓慢,因此可能会在用户返回到您的窗口时执行userCheated函数,因此有时在用户回来了。 我认为它也很好。

浏览器支持:由于某些浏览器可能无法执行此操作,因此我建议您使用jQuery插件使事情在更多浏览器上都可以使用,因为jQuery API进行了许多兼容性调整。

使用jQuery的代码将是:

$(window).onblur(userCheated);

您无法使用javascript执行此操作。 我必须使用浏览器的扩展程序来完成它,我相信Internet Explorer曾经具有禁用选项卡浏览的选项,可以浏览您希望在其中进行浏览的每种浏览器的扩展程序,也可以考虑制作一个。 更好的解决方案不是限制您不希望其查看的网站,而是禁用选项卡按钮吗?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function userCheated() {
    // The user cheated by leaving this window (e.g opening another window)
    // Do something
    alert("New Tab Opened");
}

window.onblur = userCheated;

</script>`enter code here`
</head>`enter code here`
<body>
window.onblur = userCheated;
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM