...

View Full Version : changing background image



rgn1703
01-24-2005, 06:24 PM
I created a java script that is supposed to change the background image on my page every month but it is not working as I intended (the image does not load as the background) Here is the cod I used:


function mybackground()
{
var mymonth=["JAN","FEB","MAR","APR","MAY","JUNE","JULY","AUG","SEPT","OCT","NOV","DEC"];
var mydate=new Date();
var mo=mydate.getMonth();
var monthimage="<img src='../" + mymonth[mo] +"2005.jpg'>"
document.write('monthimage')
}

and this is where the code is to be executed:


<body background="<javascript='mybackground()'>" onload="setInterval('show()',1000);">

Can anyone tell me what I did wrong and why my image will not load as the background?

Ron
:confused: :confused:

Willy Duitt
01-24-2005, 07:19 PM
For one, you can not nest an image tag within another tag...

Try this:



<script type="text/javascript">
<!--//
function mybackground(){
var mymonth = ["JAN","FEB","MAR","APR","MAY","JUNE","JULY","AUG","SEPT","OCT","NOV","DEC"];
var mydate = new Date();
var mo = mydate.getMonth();
var monthimage = '../'+mymonth[mo]+'2005.jpg';
document.getElementsByTagName('BODY')[0].style.background = 'url('+monthimage+')';
} onload = mybackground;
//-->
</script>
</head>

<body>


.....Willy

BTW: The script outputs:
<BODY style="BACKGROUND: url(../JAN2005.jpg)">
If you would like to check the output against the file name(s)...

rgn1703
01-24-2005, 08:14 PM
Thanks for the revision. I had another onload event and the page didn't load properly until I combined the two onload events.


function mybackground()
{
var mymonth=["JAN","FEB","MAR","APR","MAY","JUNE","JULY","AUG","SEPT","OCT","NOV","DEC"];
var mydate=new Date();
var mo=mydate.getMonth();
var monthimage = '../'+mymonth[mo]+'2005.jpg';
document.getElementsByTagName('BODY')[0].style.background = 'url('+monthimage+')';
} onload = mybackground; setInterval('show()',1000);

Ron

glenngv
01-25-2005, 03:22 AM
onload = function(){
mybackground();
setInterval('show()',1000);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum