It sounds like you already know how to do this, use cookies. Here is a cookie object I use:
var cookie = {
set: function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
},
get: function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
},
erase: function(name) {
cookie.set(name,"",-1);
}
}
This allows simple calls like cookie.set('cookie_name', cookie_value, days);
and cookie.get('cookie_name');
.
So, when a user selects an image, call cookie.set('bgImage', image_url, 365);
.
function changeBGImage(whichImage){
if (document.body){
document.body.style.backgroundImage = "url("+backImage[whichImage]+")";
cookie.set('bgImage', whichImage, 365);
}}
}
When your page is loading, check if the cookie exists (if(cookie.get('bgImage'))
) and change the background to the value stored in the cookie. You could just put this line at the bottom of your script block and it should work fine:
// if they have a cookie set with which image they prefer, call changeBGImage with that value.
if(cookie.get('bgImage')){
changeBGImage(cookie.get('bgImage'));
}