Skip to content Skip to sidebar Skip to footer

Javascript Change Background Based On Time

Ok, so I have been working with javascript for a little bit and Im trying to get it so that the website background changes depending on the time, Day and Night. But It seems that w

Solution 1:

Your exact code works correctly for me. Try doing: document.write(currentTime);

To see if getHours(), etc is working as expected.

Also, what happens if you set currentTime manually?

Solution 2:

put all the js codes in

window.onload=function(){
    YOURCODEHERE
}

to run them

and also,use document.body.setAttribute("style","background-image: url(/images/xxx.png);");

instead of

document.body.background=xxx;  

because when you change css style,browser will refresh element immediately.

Post a Comment for "Javascript Change Background Based On Time"