views:

178

answers:

1
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?

+1  A: 

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.

Jani Hartikainen