[英]Prevent multiple connexion to my web application
I would like to prevent multiple connexion to my web enterprise application in the same browser (window or tab) for a user. 我想防止用户在同一浏览器(窗口或标签)中多次连接到我的Web企业应用程序。
How can i do that using Javascript ? 我如何使用Javascript做到这一点?
You can create a session variable (a date and time) and store it in localStorage. 您可以创建一个会话变量(日期和时间)并将其存储在localStorage中。 Check it on window.load, and if it exists then close the window, or prevent loading further. 在window.load上检查它,如果存在,则关闭窗口,否则阻止进一步加载。
Edit: 编辑:
I created a complete solution, maybe I'll find some use for it in the future: 我创建了一个完整的解决方案,也许以后会发现它的一些用途:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<script>
(function () {
var interval = 10000; //can be modified to be faster, now it's 10 seconds
// localStorage is available everywhere
// sessionStorage is available only in current page / tab,
// so we can allow page refresh
if (localStorage.getItem("catch") && !sessionStorage.getItem("catch")) {
var date = new Date(parseInt(localStorage.getItem("catch"), 10));
if (Date.now() - date < interval) {
//prevent further loading, hide things etc.
document.write('catch');
}
} else {
setInterval(function () {
var date = Date.now();
localStorage.setItem("catch", date);
sessionStorage.setItem("catch", date);
}, interval);
}
})();
</script>
</head>
<body>
</body>
</html>
I could imagine a solution with cookies or localStorage: 我可以想象使用Cookie或localStorage的解决方案:
X
exists either as a cookie or localStorage. 检查X
是否作为cookie或localStorage存在。 If it exists, show an error and redirect to an error page. 如果存在,则显示错误并重定向到错误页面。 X
is not present, proceed. 如果不存在X
,请继续。 window.unload
and window.onbeforeunload
events) remove X
. 在页面onload( window.unload
和window.onbeforeunload
事件)上,删除X
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.