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.ge tTime() + (30 * 24 * 3600 * 1000)); // a month
function setCookie (name,value,expires,path,d omain,secu re) {
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(se arch)
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.s ubstring(o ffset, 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+"<I MG SRC='"+pics[cnt]+"' width='"+pic_width+"' height='"+pic_height+"' border='"+border_size+"'>" +cent_it2) ;
cnt++
if (cnt>=pics.length) cnt=0;
setCookie('cnt',cnt,expDat e);
//-->
</script>
<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.ge
function setCookie (name,value,expires,path,d
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(se
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";
// set index of end of cookie value
if (end == -1) end = document.cookie.length
return unescape(document.cookie.s
}
}
}
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+"<I
cnt++
if (cnt>=pics.length) cnt=0;
setCookie('cnt',cnt,expDat
//-->
</script>
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.
document.write( "<body background='images/one.gif
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>");
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+"'>"
ASKER
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
http://javascript.internet.com/bgeffects/r-bg.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
document.write( "<body background='somepicture.gi