www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript help

  1. #1
    Join Date
    Apr 2012
    Posts
    35

    Javascript help

    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

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

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

  2. #2
    Join Date
    Oct 2012
    Posts
    5
    To start off, why an object, and instead, an array? After all, that's what you basically are doing.

    Second, for bgColor you're sending a string, which it converts to a color. If that's what you want, that's well and fine.

    Finally, for your image thing, to make your image background work, you need to add "url("+...+")" to your document.body.style.background attribute, since in CSS, for a background IMAGE, you need to have it like 'url(place.com/image.jpg)' for it to work.

    In essence, like this:

    document.body.style.background="url("+cols[day]+")";

    Hope that helps.

    P.S. your title is not very descriptive. I believe in the rules it asks to have a more descriptitve title like "Cannot set background image with JS"

  3. #3
    Join Date
    Jan 2011
    Posts
    117
    Here is a as simple as powerful solution for your monthly background images.

    PHP Code:
    monthly "january.jpg,february.jpg, .  .  .  ,december.jpg".split(",");
    background = new Date().getMonth();
    document.body.background monthly[background];
    //clear used objects...
    delete background;
    delete monthly;
    //done! 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles