简体   繁体   English

如何防止用户离开javascript中的当前窗口?

[英]How to prevent user from going away from current window in javascript?

I am developing application, where user has to write certification exam online.我正在开发应用程序,用户必须在线编写认证考试。 Once user clicks start exam button, I am opening popup window to give exam.一旦用户点击开始考试按钮,我就会打开弹出窗口进行考试。 But we should not allow user to move away from this window(To prevent him from copying answer by googling in another window).但是我们不应该让用户离开这个窗口(防止他在另一个窗口中通过谷歌搜索复制答案)。 User should not be able to move away from active window in any way like pressing ALT+TAB (or) WINDOWS+D.用户不应以任何方式离开活动窗口,例如按 ALT+TAB(或)WINDOWS+D。

Basically, in javascript, we need to prevent user to move away from current window in all possible ways.基本上,在 javascript 中,我们需要以所有可能的方式防止用户离开当前窗口。 How to do it?怎么做?

Preventing users from leaving your site, is all a spamer needs.阻止用户离开您的站点,这完全是垃圾邮件发送者的需要。 Thats why it is impossible.这就是为什么这是不可能的。 The only thing you can do, is to check if the user moved away.你唯一能做的就是检查用户是否离开了。 This will stop the js execution: (See Detect if browser tab is active or user has switched away for original code)这将停止 js 执行:(请参阅检测浏览器选项卡是否处于活动状态或用户已切换到原始代码)

 var vis = (function(){ var stateKey, eventKey, keys = { hidden: "visibilitychange", webkitHidden: "webkitvisibilitychange", mozHidden: "mozvisibilitychange", msHidden: "msvisibilitychange" }; for (stateKey in keys) { if (stateKey in document) { eventKey = keys[stateKey]; break; } } return function(c) { if (c) document.addEventListener(eventKey, c); return !document[stateKey]; } })();

vis(function(){
if(vis()==false){
alert("You left the site. You try to cheat");
//do sth else
}
});

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

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