Hard to explain in the title, let me explain.
I have 3 images and I would like them to be displayed in a div and rotate everytime a user re-visits the page (They will see one image when they visit, and a different image the next time).
I was thinking of having a variable like 'count' which increments each time the user visits, and the value of count would relate to which image is displayed in this div, however I'm not sure how to save the 3 images in the local storage.
I'm expecting the logic wpould be like
setItem (Img1)
setItem (Img2)
setItem (Img3)
count = count + 1
if count = 1
div = getItem(Img1)
if count = 2
etc etc
I guess my question is:
Is there a way to store images in the local storage without first adding them to the page?
EDIT
This is now the code I'm using, however an image is not displayed, the div just displays the image location.
var counter = localStorage.getItem('counter');
if(counter == null) {
localStorage.setItem("counter", 0);
localStorage.setItem("images",'["assets/advert1.png","assets/advert2.png","assets/advert3.png"]');
$('#advert').html(JSON.parse(localStorage.getItem('images'))[0]);
}
else {
$('#advert').html(JSON.parse(localStorage.getItem('images'))[counter]);
counter++;
if(counter > 2) {
counter = 0;
}
localStorage.setItem("counter", counter);
}
(Note this is inside a document.ready function)