user2387226
user2387226

Reputation:

html5 local-storage need help setting/getting key & value

I am new to localStorage in html5, and am having some difficulties with the syntax. I am basically trying to check the value of checkFullscreen, and run some commands depending on true or false. But it's not working. What am I doing wrong, and could you provide me with a way in fixing this.

(The win.isFullscreen, win.leaveFullscreen(), win.enterFullscreen() are part of node.js)

Thank-you.

var fullscreenstore = localStorage.getItem('fullscreen');
var checkFullscreen = win.isFullscreen;

if (fullscreenstore == null || true) { // if value is null or true then fullscreen app
    localStorage.setItem(fullscreen, true);
    win.enterFullscreen();
    win.maximize();
    win.show();
}
else { // if false then run in window mode and set localStorage value false
    localStorage.setItem(fullscreen, false);
    win.maximize();
    win.show();
}

Then on pressing the 'F1' key to change the value of the localStorage value.

$(document).keyup(function (e) {
        if (e.which == 27) {
            win.close();
        };
        if (e.which == 112) {
            if (checkFullscreen == true) {
                win.leaveFullscreen();
            }
            else {
                win.enterFullscreen();
            };
        };
    });

Upvotes: 1

Views: 503

Answers (2)

Shreedhar
Shreedhar

Reputation: 5640

localStorage will convert the value into string upon storing,

e,g:

localStorage.setItem("something",true)

localStorage.getItem("something") // returns "true"

but trying to compare the value with boolean

So, string == boolean will always return false

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382150

localStorage only stores strings. Other values you set are converted to strings.

So you should compare to "true", "false" or undefined when reading values.

Upvotes: 2

Related Questions