SessionStorage 将变量布尔值设置为 NaN 或 null (Javascript)
SessionStorage setting variable boolean as NaN or null (Javascript)
当我在会话存储中设置一个项目(然后刷新页面),然后将一个变量设置为该项目时,由于某种原因它将该变量设置为 NaN 或 null,我该如何停止呢? (保存文件)
var myVar = false;
function save() {
sessionStorage.setItem("myVar", myVar)
}
function load(){
myVar = sessionStorage.getItem("myVar")
myVar = parseInt(myVar) //is supposed to set myVar as the stored variable called "myVar"
//if i do console.log() and print the variable value, it either shows up as NaN or Null
//if i didnt do 'parseInt' then the value will be 'false', but the value is in strings???
//(when value is in strings, it wont work in an "if (myVar == false){}")
}
存储在 sessionStorage
中的值是字符串。
使用它来转换为布尔值:
myVar = sessionStorage.getItem("myVar") === "true";
当我在会话存储中设置一个项目(然后刷新页面),然后将一个变量设置为该项目时,由于某种原因它将该变量设置为 NaN 或 null,我该如何停止呢? (保存文件)
var myVar = false;
function save() {
sessionStorage.setItem("myVar", myVar)
}
function load(){
myVar = sessionStorage.getItem("myVar")
myVar = parseInt(myVar) //is supposed to set myVar as the stored variable called "myVar"
//if i do console.log() and print the variable value, it either shows up as NaN or Null
//if i didnt do 'parseInt' then the value will be 'false', but the value is in strings???
//(when value is in strings, it wont work in an "if (myVar == false){}")
}
存储在 sessionStorage
中的值是字符串。
使用它来转换为布尔值:
myVar = sessionStorage.getItem("myVar") === "true";