Click to See Complete Forum and Search --> : I need to swap an image every Monday
Asgad419
04-25-2003, 02:38 PM
I need a script to swap an image every Monday. I have an image for every Monday of the year and would like an automatic alternative so I don't have to remember to switch it all the time.
I thought about using a naming convention on the image files like 20030428.gif and then parsing the filename and comparing it to today's date. But I haven't had any luck finding a way to parse the filename.
Any thoughts? Am I going about this the right way?
Thanks in advance.
khalidali63
04-25-2003, 02:46 PM
Here is an idea..
create an array of images for all the mondays in a year.
get the date.getDay()
Which will return 1 for monday and o for sunday.
if its monday{
increment and swap image
}
mae this code be executed on load of a page...
Asgad419
04-25-2003, 02:56 PM
You mention incrementing the array value, but how do I do this; how do I keep state? I can't just do an i++ because later calls of the page won't know what var i used to be will it? This whole keeping state issue is what has bombed most of my ideas.
havik
04-25-2003, 03:49 PM
I'm not sure that you could do this with javascript because you'd need some permanent variable to increment wouldn't you?
Havik
khalidali63
04-25-2003, 06:02 PM
Here you go, the script changes image every hour, I have put detailed instructions how to change images daily,weekly or monthly.
HourlyImageLoader.html (http://www.webapplikations.com/pages/html_js/image_examples/ImageRotationControlSystem.html)
The above script is tested on NS6+ and IE6+ bropwsers
Asgad419
04-28-2003, 02:39 PM
I understand how this script works for hourly, daily and monthly, but I have a few questions about weekly. Currently the script uses the
var currentTime = (new Date()).getHours();
value as an index into retreiving an image. So when the hour changes to 3:00 PM, i.e. currentTime == 15. Then image _15.jpg is retreived and put on the screen.
But when using this script for once a week, everytime it's time to change the image, currentTime will == 1. So the image retreived will always be image _01.jpg This is sufficient to know when to change the image, but not what to change the image to.
khalidali63
04-28-2003, 03:08 PM
Take a look at this line of code
imgSrc = "images/clockedimages/"+((currentTime>9)? "_"+currentTime : "_0"+currentTime)+".jpg";
This where the image file name is constructed.
The idea is to give you a logical flow to get this done,
To make this code work you only need to calculate the which week is it and then append that in the image name to create image file...
such as
2003 = getFullYear();
04 = getMonth();
4= "0"+parseInt(getDate()/7);
now you have string constructed that looks like this
2003+04+04
do you think you can load appropriate image with this???
David Harrison
04-28-2003, 03:15 PM
Simply stick this function in your head or .js file:
function changeimage(){
asgad=new Date();
day=asgad.getDay()-1;
date=asgad.getDate();
month=asgad.getMonth()+1;
year=asgad.getYear();
if(day<0){day=6;}
while(year>99){year-=100;}
monday=date-day;
if(monday<0){if(month==1 || month==2 || month==4 || month==6 || month==8 || month==9 || month==11){monday+=31;}
else if(month==5 || month==7 || month==10 || month==12){monday+=30;}
else if(month==3 && (year/4)==(Math.round(year/4))){monday+=29;}
else{monday+=28;}
month-=1;
if(month<1){month=12;year-=1;}}
if(monday<10){monday="0"+monday;}
if(month<10){month="0"+month;}
if(year<10){year="0"+year;}
document.write("<img src='"+monday+""+month+""+year+".gif'>");}
and the on your page put:
<script language="javascript">changeimage();</script>
If like this week the monday was on the 28th April 2003, the script would load 280403.gif
Does this do what you want? If you want me to explain why I've done something, just ask.
The only problem is that someone could skip ahead a picture if they wanted just by changing the date on their PC.
Asgad419
04-28-2003, 03:32 PM
Ooo, nice. That's just what i was looking for. Even works for leapyear.
David Harrison
04-28-2003, 03:52 PM
I aim to please.
Norlos
07-26-2003, 09:48 PM
The script you folks provide is very close to one I'm looking for. I want one that will either
1) Cycle through 8 to 10 images everytime the site is visited or reloaded
2) Pick one of the eight randomly and post it.
Any thoughts? Or anything already available?
David Harrison
07-27-2003, 04:36 PM
Well here's a script that will cycle through four images and back to the start again, when the user clicks on it. You could easily modify it by running the function in an onload event handler and adding more images into the array.
I'm not entirely sure what you mean when you say:Pick one of the eight randomly and post it.
Norlos
07-28-2003, 05:10 AM
Thanks. I'll take a look.
I meant that I'd like a script that provides a different image (among a set of eight or so) every time the site is visited.