[英]Variables not saving after reloading a page
當我運行下面的代碼時,如果登錄成功,它通常會在 testUses 中添加一個數字。 但是,當我重新加載時,您仍然可以訪問 home.html 並且變量 testUses 仍然保持為 0,這意味着由於某種原因,它沒有改變。 我應該把 testUses 放在 function 之外嗎? 請回答修復或提示以更改此設置。
<!DOCTYPE html>
<html>
<head>
<script>window.history.replaceState(null, document.title, "/");</script>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<title>A-ENGINE</title>
<meta name="viewport" content="width=device-width">
<link href='https://fonts.googleapis.com/css?family=Quicksand' rel='stylesheet'>
<style>
.full {
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: absolute;
overflow: hidden;
}
.center {
left: 50%;
top:50%;
transform: translate(-50%, -50%);
position: absolute;
}
</style>
</head>
<body style='overflow:hidden;padding:0px;visibilty:hidden'>
<div class='full'>
<div class='center' style='width:300px;padding:2px'>
<div class="cursor"><div class="planet"></div></div>
<input type= password id="access" placeholder="Access Key" onchange="access()"><br>
<script>
function access() {
let input = document.getElementById("access").value;
let testUses = +localStorage.getItem('testUses') || 0;
const testHash = "cyrb53('a') -> 4625896200565286";
const adminHash = "cyrb53('a') -> 4413594719508086";
const cyrb53 = (str, seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
let inputHash = (`cyrb53('a') -> ${cyrb53(input)}`);
function home() {
document.write('<iframe src="home.html" height="100%" width="100%"></iframe>');
}
if (inputHash == adminHash || inputHash == testHash) {
home();
} else if (inputHash == testHash && testUses <= 1) {
home();
testUses++;
localStorage.setItem('testUses', testUses);
} else {
document.getElementById('access').value = '';
}
}
</script>
</div>
</body>
</html>
您可以將testUses
存儲在localStorage
中。
let testUses = +localStorage.getItem('testUses') || 0;
// update value:
testUses++;
localStorage.setItem('testUses', testUses);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.