Javascript 设置并检查localstorage的特定值,然后根据数据执行函数

Javascript 设置并检查localstorage的特定值,然后根据数据执行函数,javascript,html,function,local-storage,Javascript,Html,Function,Local Storage,我试图从一个页面(“index.html/settest.html”)设置本地存储,并在“index.html”上检查它。如果检查返回某个结果,它将执行一个函数 我已经为此编写了一些代码,但它不起作用。我真的不知道为什么,所以我希望在这里得到一些帮助 下面是我在“settest.html”页面上的内容。真的很简单- <script> window.onload=setlocalstorage() { localS

我试图从一个页面(“index.html/settest.html”)设置本地存储,并在“index.html”上检查它。如果检查返回某个结果,它将执行一个函数

我已经为此编写了一些代码,但它不起作用。我真的不知道为什么,所以我希望在这里得到一些帮助

下面是我在“settest.html”页面上的内容。真的很简单-

        <script>
            window.onload=setlocalstorage() {
                localStorage.setItem("one", true);
            }
        </script>

window.onload=setlocalstorage(){
setItem(“一”,true);
}
因此,按照我的理解,当页面加载时,应该在localStorage中将“one”的值设置为true

以下是我在“index.html”页面上的内容-


window.onload=setInterval(函数(){
var one=localStorage.getItem('one')| |“”;
如果(一个!=“是”){
函数hideone(){
var elem=document.getElementById(“一”);
elem.className=“隐藏”;
}
}
}, 1000);
据我所知,这应该每秒检查localStorage的“one”,如果返回yes(或true),则执行函数“hideone”

然而,当我转到“settest.html”,然后访问“index.html”时,什么也没有发生。控制台中没有错误,也没有任何异常显示。我只是不明白为什么它不起作用

提前感谢,如果有人需要更多信息或上下文,请随时询问


-Mitchyl

您没有定义
窗口。onload
功能正确。要么:

<script>
    window.onload = function() {
        localStorage.setItem("one", true);
    }
</script>

此外,您正在第一页上将
localStorage.one
设置为
true
,并在另一页上检查它是否为
yes
。不确定这是错误还是错误。

没有正确定义任何函数。看起来您想这样做:

settest.html:

<script>
    window.onload=function()
    {
        localStorage.setItem("one", true);
    }
</script>

window.onload=function()
{
setItem(“一”,true);
}
index.html:

<script>
    window.onload = function ()
    {
        setInterval(function()
        {
            var one = localStorage.getItem('one') || '';
            if (one !== true)
            {
                var elem = document.getElementById("one");
                elem.className = "hide";
            }
        }, 1000);
    }
</script>

window.onload=函数()
{
setInterval(函数()
{
var one=localStorage.getItem('one')| |“”;
如果(一!==真)
{
var elem=document.getElementById(“一”);
elem.className=“隐藏”;
}
}, 1000);
}

假设您希望它每秒钟检查一次,看看是否需要将特定元素上的类设置为“隐藏”。

您似乎错误地定义了函数。第一个window.onload只是语法错误?废话。谢谢,伙计们,我知道这很简单。天哪,我觉得自己像个傻瓜。谢谢你们的帮助,伙计们!我不知道我在这方面是否正确,但我被告知“是”和“真”是相等的。不过我已经更改了。字符串
yes
true
不相等。执行相等性检查:在浏览器控制台中,键入
'true'==='yes'
。应该返回false。但是,当使用
if
语句进行计算时,“yes”(非空字符串)表面上等于
true
。不过,在这两种情况下,您仍然应该使用
true
。谢谢你的信息!我实际上学到了很多东西。谢谢你的回答:)是的。我只是在落实所有的事情,确保我没有多余的问题。再一次,非常感谢。
window.onload = function() {
    setInterval(function() {
        var one = localStorage.getItem('one') || '';
        if (one != 'yes') {
            function hideone() {
                var elem = document.getElementById("one");
                elem.className = "hide";
            }
        }
    }, 1000);
};
<script>
    window.onload=function()
    {
        localStorage.setItem("one", true);
    }
</script>
<script>
    window.onload = function ()
    {
        setInterval(function()
        {
            var one = localStorage.getItem('one') || '';
            if (one !== true)
            {
                var elem = document.getElementById("one");
                elem.className = "hide";
            }
        }, 1000);
    }
</script>