gpt4 book ai didi

javascript - 防止多重连接到我的 Web 应用程序

转载 作者:行者123 更新时间:2023-11-30 17:48:08 25 4
gpt4 key购买 nike

我想防止用户在同一个浏览器(窗口或选项卡)中多次连接到我的 Web 企业应用程序。

我如何使用 Javascript 做到这一点?

最佳答案

您可以创建 session 变量(日期和时间)并将其存储在 localStorage 中。在 window.load 上检查它,如果它存在则关闭窗口,或阻止进一步加载。

编辑:

我创建了一个完整的解决方案,也许我将来会找到一些用处:

<!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>

关于javascript - 防止多重连接到我的 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com