Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7625507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:12:04+00:00 2026-05-31T05:12:04+00:00

I’m just starting to learn javascript, so this is likely a pretty simple question.

  • 0

I’m just starting to learn javascript, so this is likely a pretty simple question. I’ve tried searching for an answer, but I think I just don’t know enough to be like, ‘oh, this looks different but answers my question too.’

What I want to be able to do is to change the background color of a page based on the time of day, and also change an image based on the time of day. I have it working with an if…else if… statement for the background color placed in the head of the page, and a separate if…else if… statement affecting the image in the body.

The head script that changes the bg color looks like:

var d=new Date();  
var time=d.getHours();  

if (time>=0 && time<=5)  
{  
document.write ('&lt;body style="background-color: 296688">')  
}  
else if  

…and then the other times follow, each with a different color.

The body script that changes the image looks like:

<img src="" name="sunMoon" id="sunMoon" />  
    <script type="text/javascript">  
    var d=new Date();  
    var time=d.getHours();  
    var elem = document.getElementById('sunMoon')  
if (time>=0 && time<=5)  
        {  
        elem.src = 'Images/sunMoon1.png'  
        }  
    else if

…and then the other times follow, each with a different src.

Is it possible to change the image AND the bg color using the same if…else if… statement in the head? I tried something like this in the head:

var d=new Date();
var time=d.getHours();
var elem=document.getElementById('sunMoon')

    if (time>=0 && time<=5)
    {
    document.write ('<body style="background-color: 2966B8">');
            elem.src="images/sunMoon1.png"
    }
else if...

but it didn’t work.

I think with the third (nonworking) example, either it’s not possible to have a single if… do two things (change the bg color AND the image), or I’m just messing up the image code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:12:05+00:00Added an answer on May 31, 2026 at 5:12 am

    The problem with your third (non-working) bit of code is that you are trying to change the source attribute of an element that doesn’t exist when the script is run. Generally speaking the browser parses the page’s HTML from top to bottom, when it comes across a block it runs the code then continues on through the HTML. So in the example you have working the JavaScript code that alters the image source comes after the element in the HTML which is why it works. In the broken code you call elem=document.getElementById('sunMoon') in the head section, so no element with ID ‘sunMoon’ exists yet.

    Generally it is best practice to place your scripts at the bottom of the page (just before the closing tag) so that they run after everything else has loaded, and don’t block the rendering of the page. If you were to do that you’d need to change the code that alters the background colour as you can’t write it directly to the body tag. The best practice solution would be to apply a different CSS class to the body depending on the time of day and then set up style rules for each class in your CSS.

    So for example:

    <script type="text/javascript">
    
      var body = document.getElementsByTagName('body')[0];
      var elem = document.getElementById('sunMoon');
    
      if (time>=0 && time<5)  
      {  
        body.className = 'morning';
        elem.src = 'Images/sunMoon1.png';
      }  
      else if (time>=5 && time<10)  
      {  
        body.className = 'afternoon';
        elem.src = 'Images/sunMoon2.png';
      }
    
    </script>
    

    Then in your CSS you need rules for .morning, .afternoon etc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.