Search code examples
javascriptjqueryhtmlsession-storagesessionstorage

Why my sessionStorage doesn't work


just short question, why this:

<link rel="stylesheet" id="style" href="style.css">

<script>

document.getElementById('style').setAttribute('href', sessionStorage.getItem('style');

</script>

Doesn't work??

I guess my session storage is right, becouse with developer tools i can see that item style has a value, and when i change this value with other code it works, style actually changes, but when i open site again, eventhough the session storage value is ok the style doesn't change...

Anything? Thanks


Solution

  • You forgot a closing ):

    document.getElementById('style').setAttribute('href', sessionStorage.getItem('style'));
    

    or to improve readability:

    var style = sessionStorage.getItem('style');
    document.getElementById('style').setAttribute('href', style);
    

    Also make sure that you have previously stored a value in sessionStorage:

    sessionStorage.setItem('style', '/foo.css');