如何为 JavaScript 变量分配本地存储变量?
这是我的尝试:
var abc=localStorage.crabc;
function tab()
{
if (abc==2) {
window.open("index.html","_self")
} else {
window.open("main.html","_self")
}
}
tab()
localStorage.crabc="2";
如何为 JavaScript 变量分配本地存储变量?
这是我的尝试:
var abc=localStorage.crabc;
function tab()
{
if (abc==2) {
window.open("index.html","_self")
} else {
window.open("main.html","_self")
}
}
tab()
localStorage.crabc="2";
tab()
您需要使用localStorage.getItem()
andlocalStorage.setItem()
来检索和存储项目到 localStorage:
var abc=localStorage.getItem('crabc');
function tab()
{
if (abc=='2') {
window.open("index.html","_self")
} else {
window.open("main.html","_self")
}
}
tab();
localStorage.setItem('crabc', '2');
在这里试试这个演示
function tab() {
var check = window.localStorage.getItem("visitedBefore");
if (check) {
alert('already visited');
window.open("index.html", "_self")
} else {
alert('first time visited');
window.open("main.html", "_self")
}
window.localStorage.setItem("visitedBefore", true);
}
HTML:
<input type="button" value="redirect" onclick="tab()" />