Skip to content Skip to sidebar Skip to footer

Can't Get Sessionstorage To Work Correctly

I have multiple buttons that when they are clicked an image is loaded and the image is supposed to stay there based even when the page refreshes. When I use this the button with th

Solution 1:

In your session storage, you are setting the value of the 'isImage1' Item to '2'

sessionStorage.setItem('isImage1', '2');

But in your code to retrieve the value you are actually retrieving the item '2'

var isImage1 = sessionStorage.getItem('2');

You need to change your sessionStorage.getItem to reference 'isImage1'

var isImage1 = sessionStorage.getItem('isImage1');

Then you should get the value you are expecting.

There are loads of good jsfiddles on session storage. you may get some ideas from this one:

http://jsfiddle.net/gabrieleromanato/XLRAH/

Incidently; this is a very small value you are storing, why not store it in a cookie instead?

EDIT:

based on the fact that you have multiple functions exactly like this one, you are better off following Ken's solution, the only thing I would add is a wildcard to turn off the other images:

function showImage(imgNum) {
            sessionStorage.setItem('Image',imgNum);
            $("[id^=loadingImage]").hide();
            $("#loadingImage" + imgNum).show();

}

showImage(sessionStorage.getItem('Image'));

The code in the buttons would then be showImage(1) instead of showImage1();

_Pez

Post a Comment for "Can't Get Sessionstorage To Work Correctly"