I am trying to append images into local storage.
I have a div that I would like to hold all these images. Something as simple as:
<div id="imageContainer">
<!--have images go here-->
</div>
What I am trying to accomplish is something like this:
if (Math.floor((Math.random()*3)+1) == 1) { // 1/3 chance of conditional being true
// append some image to #imageContainer and have it locally stored
} else {
alert('no luck this time...');
}
So, each time that above condition is true, how would I append an image into #imageContainer and have it locally saved?
If the images are small, you could store the image in localStorage as base64 encoded data URI. For example, here is the StackOverflow image as a data URI:
So you would show that in an image like this:
All you would have to do is create an
<img>and set thesrcattribute to the data URI.