<!-- If you can name your image files, and suffix each with an integer,
you can make it simpler: -->
<script>
function mondayImage(){
var n, now= new Date(), d= new Date(now.getFullYear(), 0, 0);
while(d.getDay()!== 1) d.setDate(d.getDate()+1);
n= Math.floor((now-d)/6.048e8);
document.getElementById('mondayimage').src= '/path/to/image.image'+n+'.jpg';
}
window.onload=mondayImage;
</script>
</head>
<body>
<img id='mondayimage' src="" alt="monday image">
The first thing we need to do is to work out how we define when a week starts and finishes and how we determine which week a particular date is in. The getDay() method returns a value of 0 for Sunday and 6 for Saturday so let's start by defining our week as running from Sunday to Saturday. The 1st January can fall on any day of the week and so not all of the days in that week will be in the current year but we'll consider the week that contains the 1st January to be week one of the current year even though not all of the days in that week are necessarily in the current year. So week one consists of those days between 1st January and the first Saturday on or after that date and the seven days following that make up week two and so on. Once we get to the end of December the last few days of the year will be in week 53 (or possibly 54 if it is a leap year starting on a Saturday).
I wonder about Leap Year 2012 and the fact that days, months, etc. start with zero.
So if you just want an image to rotate by the hour, put in "getHour" instead?
And if you want it to loop, add the comma at the end, right?
I'm just learning, but I want to make sure...
Thanks!
Which post # code are you asking about? There were two solutions.
What does your attempt look like?
It's always a good idea to try the modifications yourself so that you can learn from trying!
If they don't work, then at least we know what you are trying to do with YOUR code.
Probably easiest modification would be to get the week of the year
and do a divide by 2 to determine image or message to display.
pseudo code: (assumes using something similar to post #2)
var today = new Date();
var weekOfYear = today.getWeek();
var imgPointer = Math.floor(weekOfYear/2);
// use image pointer to point to element of image array to display
document.getElementById('WeeklyImage').src = imgList[imgPointer];
so if you want to rotate it by every 14 days you can just do % 14, that will give you number on what you can base rest of your logic
I read the original request that the image or message would change every 2 weeks, ie;
stable message for a 2 week period and then changed.
This assumption is based on post #8 and #10 where OP originally referred to post #2 code, I thought.
Your code seems to change every day for a 4 day, if you % 4,
or a 2 week period, if you % 14, and then repeats the images or messages.
What ever the OP wants is fine, but it would be nice to know what the exact requirements are as the request is somewhat vague!
Ah, it starts with 0!
Any way to make it start with 1?
(I have others who will be populating the images, and I'd like to K.I.S.S.)
And how can I also make this change sequential text files on Monday for virtual SSI includes?
I tried this, but it didn't work. I'm sure I've got something totally wrong:
<script>
function mondayText(){
var n, now= new Date(), d= new Date(now.getFullYear(), 0, 0);
while(d.getDay()!== 1) d.setDate(d.getDate()+1);
n= Math.floor((now-d)/6.048e8);
document.getElementById('mondaytext').src= '/text'+n+'.txt';
}
window.onload=mondayText;
</script>
</head>
Bookmarks