Link to home
Start Free TrialLog in
Avatar of azteccoupe
azteccoupe

asked on

Change background image on refresh

The following script swaps a series of images everytime the page is refreshed. Can the script be configured to swap background images on refresh?


<script language="JavaScript">
<!--

pic_width=200;
pic_height=200;
border_size=0;
alignment=true;



/* define image urls */
pics = new Array(
"images/one.gif",
"images/two.gif",
"images/three.gif",
"images/four.gif",
"images/five.gif"
);

expDate = new Date ();
expDate.setTime(expDate.getTime() + (30 * 24 * 3600 * 1000)); // a month

function setCookie (name,value,expires,path,domain,secure) {
   var theCookie = name + "=" + escape (value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path)    ? "; path="    + path   : "") +
   ((domain)  ? "; domain="  + domain : "") +
   ((secure)  ? "; secure"            : "");
   document.cookie = theCookie;
}

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists
         offset += search.length
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset)
         // set index of end of cookie value
         if (end == -1) end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }
   }
}

cent_it="";
cent_it2="";
if (alignment) {
  cent_it="<CENTER>";
  cent_it2="<\/CENTER>";
}  

cnt = getCookie('cnt');
if (isNaN(cnt)) cnt = 0;
if (document.images)
{
     pic= new Image();
     pic.src=pics[cnt];
}
document.write(cent_it+"<IMG SRC='"+pics[cnt]+"' width='"+pic_width+"' height='"+pic_height+"' border='"+border_size+"'>"+cent_it2);
cnt++
if (cnt>=pics.length) cnt=0;
setCookie('cnt',cnt,expDate);

//-->
</script>

Avatar of rolftollerud
rolftollerud

add to the document.write line,

document.write( "<body background='somepicture.gif'>" + cent_it+"<IMG SRC='"+ pics[cnt]+"' width='"+pic_width+"' height='"+pic_height+"' border='"+border_size+"'>"+cent_it2 + "<\/body>");
Avatar of azteccoupe

ASKER

I replaced the document.write line with:

document.write( "<body background='images/one.gif'>" + cent_it+"<IMG SRC='"+ pics[cnt]+"' width='"+pic_width+"' height='"+pic_height+"' border='"+border_size+"'>"+cent_it2 + "<\/body>");

this adds the image to the background but it does not swap on refresh.
you just have to name the background pictures, for instance

pics_bg = new Array(
"images/one_bg.gif",
"images/two_bg.gif",
"images/three_bg.gif",
"images/four_bg.gif",
"images/five_bg.gif"
);

then,
document.write( "<body background=' " + pics_bg[cnt] + " '>" + cent_it+"<IMG SRC='"+ pics[cnt]+"' width='"+pic_width+"' height='"+pic_height+"' border='"+border_size+"'>"+cent_it2 + "<\/body>");
I don't know what I am doing wrong ... I renamed my images just like in your example (one_bg.gif etc) and inserted the new document.write but I get nothing. The images won't even display :(
Here's another idea, a separate script that loads a random background...

http://javascript.internet.com/bgeffects/r-bg.html
ASKER CERTIFIED SOLUTION
Avatar of rolftollerud
rolftollerud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial