Opera localStorage ignores my existence check
-
23-09-2019 - |
문제
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?
해결책
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.
제휴하지 않습니다 StackOverflow