Opera localStorage 忽略我的存在检查
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
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.