如何从 JavaScript 中的另一个文件访问对象?
How to access an object from another file in JavaScript?
我想将玩过的游戏的分数存储在本地存储中topScore 旁边的usrObj 中。但是,它仅将分数存储在用户名旁边。
来自 index.html
的 js 代码
<script>
loadRankingTable();
window.onload = ()=> {
//Check login
if(sessionStorage.loggedInUser !== undefined){
window.localStorage.setItem(sessionStorage.loggedInUser, highscore);
document.getElementById("Greeting").innerHTML = sessionStorage.loggedInUser;
}
}
</script>
prac.js
function register(){
let password1 = document.getElementById("PasswordInput").value
let password2 = document.getElementById("PasswordInputConfirm").value
let username = document.getElementById("Username").value
let name = document.getElementById("NameInput").value;
let password = document.getElementById("PasswordInput").value;
let usrObj = {
username: username,
password: password,
topScore: 0
}
if (password1 == password2){
localStorage[name] = JSON.stringify(usrObj);
document.getElementById("FeedbackPara").innerHTML= "";
}
else{
document.getElementById("FeedbackPara").innerHTML= "Passwords don't match.";
}
}
所以基本上不是像这样存储数据的本地存储:“用户,200”,我希望它是这样的:“用户名:用户名,密码:密码,topScore:200”
获取本地存储的当前值作为JSON,更新分数,然后再次保存。
loadRankingTable();
window.onload = () => {
//Check login
if (sessionStorage.loggedInUser !== undefined) {
let oldData = localStorage.getItem(sessionStorage.loggedInUser);
if (oldData) {
oldData = JSON.parse(oldData);
oldData.topScore = highscore;
localStorage.setItem(sessionStorage.loggedInUser, JSON.stringify(oldData);
}
document.getElementById("Greeting").innerHTML = sessionStorage.loggedInUser;
}
}
我想将玩过的游戏的分数存储在本地存储中topScore 旁边的usrObj 中。但是,它仅将分数存储在用户名旁边。
来自 index.html
的 js 代码<script>
loadRankingTable();
window.onload = ()=> {
//Check login
if(sessionStorage.loggedInUser !== undefined){
window.localStorage.setItem(sessionStorage.loggedInUser, highscore);
document.getElementById("Greeting").innerHTML = sessionStorage.loggedInUser;
}
}
</script>
prac.js
function register(){
let password1 = document.getElementById("PasswordInput").value
let password2 = document.getElementById("PasswordInputConfirm").value
let username = document.getElementById("Username").value
let name = document.getElementById("NameInput").value;
let password = document.getElementById("PasswordInput").value;
let usrObj = {
username: username,
password: password,
topScore: 0
}
if (password1 == password2){
localStorage[name] = JSON.stringify(usrObj);
document.getElementById("FeedbackPara").innerHTML= "";
}
else{
document.getElementById("FeedbackPara").innerHTML= "Passwords don't match.";
}
}
所以基本上不是像这样存储数据的本地存储:“用户,200”,我希望它是这样的:“用户名:用户名,密码:密码,topScore:200”
获取本地存储的当前值作为JSON,更新分数,然后再次保存。
loadRankingTable();
window.onload = () => {
//Check login
if (sessionStorage.loggedInUser !== undefined) {
let oldData = localStorage.getItem(sessionStorage.loggedInUser);
if (oldData) {
oldData = JSON.parse(oldData);
oldData.topScore = highscore;
localStorage.setItem(sessionStorage.loggedInUser, JSON.stringify(oldData);
}
document.getElementById("Greeting").innerHTML = sessionStorage.loggedInUser;
}
}