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 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    background colour change on start of each month using javascript

    Hello

    I need bit of help

    I am trying to use javascript to change the website background image on the start of each month

    So far I have this in between the head tags

    <script>
    /*
    SM_dailyBgColor()
    This function automatically changes the page's background color each day of the week.
    Author: Sean McManus
    Author URL: http://www.sean.co.uk/
    Usage: SM_dailyBgColor ("januarycolor", "februarycolor", "marchcolor", "aprilcolor", "maycolor", "junecolor", "julycolor", "augustcolor", "septembercolor", "octobercolor", "novembercolor", "decembercolor");
    */

    function SM_dailybg(january,february,march,april,may,june,july,august,september,october,november,december)
    {
    cols=new Object();
    cols[0]=january;
    cols[1]=february;
    cols[2]=march;
    cols[3]=april;
    cols[4]=may;
    cols[5]=june;
    cols[6]=july;
    cols[7]=august;
    cols[8]=september;
    cols[9]=october;
    cols[10]=november;
    cols[11]=december;

    today=new Date();
    day=Math.round(today.getMonth());
    document.bgColor=cols[day];
    document.body.style.background=cols[day];
    }

    </script>


    And the below in between my body tags

    <body onLoad='SM_dailybg("images/1.jpg", "images/background.png", "images/1.jpg", "images/background.png", "images/1.jpg", "images/background.png", "images/1.jpg","images/background.png","images/1.jpg","images/background.png","images/1.jpg","images/background.png");'

    Can some please help me and point me in the right direction to get it to work

    Kind regards

    Ian
    Last edited by ianhaney; 10-14-2012 at 01:53 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    
    var images = ["jan.jpg", "feb.jpg", "mar.jpg", "apr.jpg", "may.jpg", "jun.jpg", "jul.jpg", "aug.jpg", "sep.jpg", "oct.jpg", "nov.jpg", "dec.jpg"];
    
    var mth = new Date().getMonth();
    var backgroundpicture = images[mth];
    document.body.background = backgroundpicture;
    
    </script>
    Or

    Code:
    <script type = "text/javascript">
    
    var images = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
    
    var mth = new Date().getMonth();
    var backgroundpicture = images[mth] + ".jpg";
    document.body.background = backgroundpicture;
    
    </script>

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. Everybody here wants Javascript help. You can (and should) edit it to make it more meaningful.


    “Always acknowledge a fault. This will throw those in authority off their guard and give you an opportunity to commit more.” - Mark Twain
    Last edited by Philip M; 10-14-2012 at 12:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Philip M

    Will change the title now to something more specific

    Thank you for the coding

    I decided to use your first coding

    Code:
    <script type = "text/javascript">
    
    var images = ["jan.jpg", "feb.jpg", "mar.jpg", "apr.jpg", "may.jpg", "jun.jpg", "jul.jpg", "aug.jpg", "sep.jpg", "oct.jpg", "nov.jpg", "dec.jpg"];
    
    var mth = new Date().getMonth();
    var backgroundpicture = images[mth];
    document.body.background = backgroundpicture;
    
    </script>
    I put that in between the head tags

    Do I need to put anything in between the body tags

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by ianhaney View Post
    I put that in between the head tags

    Do I need to put anything in between the body tags

    You should place the script immediately before the </body> tag. The <body> background cannot be accessed before it exists.

    Thank you for editing the thread title.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much for all the help Philip really appreciate it

    Will try it out now

    That's ok regarding the title, thank you for helping me

    Ian


  •  

    Posting Permissions

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