Opera localStorage 忽略我的存在检查

发布于 2024-08-19 12:35:23 字数 336 浏览 4 评论 0原文

if (localStorage)
{
    var menushown = JSON.parse(localStorage.getItem("menuState"));
    if (!menushown)
    {
        menushown = menuDefaultState;
    }
} 
else
{
    menushown = menuDefaultState;
}

上述 JavaScript 在 Opera 10.10 中失败。它抱怨第二行的 localStorage 未定义。这是事实,但由于对第一行的检查,它永远不应该到达第二行,并且不会失败。 我做错了什么?

if (localStorage)
{
    var menushown = JSON.parse(localStorage.getItem("menuState"));
    if (!menushown)
    {
        menushown = menuDefaultState;
    }
} 
else
{
    menushown = menuDefaultState;
}

The above JavaScript fails in Opera 10.10. It complains that localStorage is undefined on the second line. This is true, but because of the check on the first line it should never reach the second line, and not fail.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-08-26 12:35:23

使用 if(window.localStorage),您当前的代码可能会在不兼容的浏览器中导致警告或错误。

localStorage 自 10.50 起仅在 Opera 中可用,至少对我来说,它在您的代码中运行得很好。

Use if(window.localStorage), your current code will probably cause a warning or an error in non-compatible browsers.

localStorage is only available in Opera since 10.50, and at least for myself it works quite fine there with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文