使用 JavaScript,是否可以将页面上的所有变量保存到本地存储,然后在页面刷新或重新加载时重新加载存储的变量?我正在尝试将变量保存到本地存储并在页面刷新时加载它们。这是我迄今为止尝试过的:
function saveAllVariables(){
//save all variables on the page to local storage
}
function loadAllVariables(){
//load all variables on the page from local storage, and re-create them using their original names
}
loadAllVariables(); //load all variables that were previously stored
if(typeof theName == "undefined"){
var theName = prompt("Please enter your name:","Your name");
}
if(typeof currentTime == "undefined"){
var currentTime = new Date();
}
document.body.innerHTML += "Time last visited: " + currentTime + "<br />";
document.body.innerHTML += "Your name : " + theName + "<br />";
var currentTime = new Date();