Can someone help me fix this code? I am trying to change images on my webpage base on time and day of the week. For example lets say today is monday i want an image to display from 9am to 12 pm and tuesday and so on. Please help
<html>
<body>
<script type="text/javascript">
var day = new Date();
var today = day.getDay();
var hour = day.getHours();
var minutes = day.getMinutes();
if (today>0 && today<6)
{if (hour = 8)
{if (minutes > 23)
{document.write("<img src="image2.jpg">")}
else
{document.write("<img src="image1.jpg">")}
}
else if (hour>8 && hour<14)
{document.write("<img src="image2.jpg">")}
else if (hour = 2)
{if (minutes < 43)
{document.write("<img src="image2.jpg">")}
else
{document.write("<img src="image1.jpg">")}
}
else
{document.write("<img src="image1.jpg>")}
</script>
</body>
Did you create IMG element with id āimageā inside of body tag and did you place this script at the bottom?
Are you images placed in the same directory as the index.html file?
Try with some images from the web instead of you image1 and image2.
Sorry for diverting the thread, but I saw this and a few other similar lines of code above:
if (hour = 8) {
I originally commented in the OPs original code in the PHP board (but then deleted it) that the line wonāt work correctly because it should have a double-equal sign. Iām learning JS and thought I was correct, but as the code above apparently works correctly, am I wrong about the = / == difference in JS?
Yes you are correct the single ā=ā is an assignment operator and assigns the value on the right to the variable on the left.
Therefore this code:
if (hour = 19) {
alert(hour);
} else {
//this will never be executed
alert('help');
}
The above will always result in the hour being 19 because thatās what you suddenly assign to it and so it always will be true.
On the other hand the double == is a comparison operator and will compare the value on the right to the variable on the leftā¦
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var minutes = date.getMinutes();
var img;
if (hour == 19) {
do something
} else {
do something else
}
The following code will only parse the first section (do something) when the actual hour is 19.
Youāre absolutely right. That part of code propagated from the first post
I didnāt see it because I assumed that conditions are ok and that part of code with inserting image to DOM is critical.
Can you please help me fix my code jasmin? I am trying to display an image at day of the week and time. For example today is monday a image will show from 7am to 10am and tuesdayā¦etc please help me
Soā¦ the main problem here is how and when the function that changing images is going to triggered.You have two choices
One: when the page loading then it triggers the function and then the function set the image src
Two:Dynamically to add a function like setInterval() that executing (in my case every 60000 ms=60sec=1min).In this case the function that contained inside the setInterval() function wrapper will check the time and correspondingly change the img src attr.
So my suggestion is the following:
For the first choise:
HTML code
<img src="" alt="" id="image_1" />
Javascript code:
var day = new Date();
var today =parseInt(day.getDay());
var hour = parseInt(day.getHours());
var minutes = parseInt(day.getMinutes());
var imgElem=document.getElementById('image_1');
(function(){
if(today>=0 && today<=6){
if(minutes>=0 && minutes<23){
imgElem.src='url_1.jpg';
}else if(minutes>=23 && minutes<45){
imgElem.src='url_2.jpg';
}else{
imgElem.src='url_3.jpg';
}
}
})();
For the second choise:
The same as above HTML code
Javascript:
Yes.Make a test in your page changing the date and hour minutes values to see if its working.Eg if you have now 20:45 set date 2 hour 20 minute 47 and check how ots working