Search code examples
javascriptsession-storage

Display stored images


I have some variables sessionStorage containing sources of images, and what i should do is displaying them. In my HTML page I tried like this:

</div>
<h1 id="h1poggia">
    Here's your choices:
</h1>
<div>
    <script src="Script1.js">
        riep();
    </script>
</div>

And this is the funcion:

    function riep() {
    imuno = sessionStorage.getItem('imuno');
    imdue = sessionStorage.getItem('imdue');
    document.write(imuno, imdue);
}

But it seems the function doesn't even get retrieved! Where am I wrong?


Solution

  • This doesn't works:

    <script src="Script1.js">
        riep();
    </script>
    

    If you need to include an external script and then call to function maybe you need to separate that:

    <script src="Script1.js"></script> 
    <script>
        riep();
    </script>
    

    However you are using document.write that's dangerous and deprecated. Please, use this instead:

    document.getElementById("myDiv").innerHTML = imuno+", "+imdue;
    

    And set an ID to the div:

    <div id="myDiv"></div>