Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question changing background image

    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:

    Code:
    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:

    Code:
    <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

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For one, you can not nest an image tag within another tag...

    Try this:

    Code:
    <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)...
    Last edited by Willy Duitt; 01-24-2005 at 08:22 PM.

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the revision. I had another onload event and the page didn't load properly until I combined the two onload events.

    Code:
    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

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    onload = function(){ 
      mybackground();
      setInterval('show()',1000);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •