Image rotation with javascript help needed
15 posts
• Page 1 of 2 • 1, 2
Image rotation with javascript help needed
here is how to do it.... I have questions though..
- Code: Select all
function adArray() {
for (i=0; i*2<adArray.arguments.length; i++) {
this[i] = new Object();
this[i].src = adArray.arguments[i*2];
this[i].href = adArray.arguments[i*2+1];
}
this.length = i;
}
function getAdNum() {
dat = new Date();
dat = (dat.getTime()+"").charAt(8);
if (dat.length == 1)
ad_num = dat%ads.length;
else
ad_num = 0;
return ad_num;
}
var ads = new adArray(
"/artists/portfolios/brandon/IMG_1.jpg","http://www.provocativetattoo.com",
"/artists/portfolios/brandon/IMG_2.jpg","http://www.provocativetattoo.com");
var ad_num = getAdNum();
document.write('<A HREF="'+ads[ad_num].href+'"><IMG SRC="'+ads[ad_num].src+'" WIDTH="300" HEIGHT="300"></A>');
link_num = document.links.length-1;
function rotateSponsor() {
if (document.images) {
ad_num = (ad_num+1)%ads.length;
document.js_ad.src = ads[ad_num].src;
document.links[link_num].href = ads[ad_num].href;
setTimeout("rotateSponsor()",604800000);
}
}
setTimeout("rotateSponsor()",604800000);
okay right at the end the line
- Code: Select all
setTimeout("rotateSponsor()",604800000);
should rotate the image every week. I figured that if 5000 = 5 seconds, then 604800000 = 1 week...
except it does not seem to be working.
i need the image to rotate every week.
You need to set a cookie variable otherwise with each page refresh it will cause it to set a new date/time for the week. This script would only work if the page was opened once and left opened for at least 1 week.
I don't have an example right now but I can work one up tomorrow.
- jbullard
- VP - Software
- Posts: 3653
- Joined: Sun Jun 06, 2004 10:17 pm
- Location: Utah
- Real Name: Jason Bullard
Actually I was mistaken. You don't need cookies. You just need to rotate it with each week so the best solution would be to do something like this.
http://www.mikrosight.com/scripts/weekl ... mage.shtml
http://www.mikrosight.com/scripts/weekl ... mage.shtml
- jbullard
- VP - Software
- Posts: 3653
- Joined: Sun Jun 06, 2004 10:17 pm
- Location: Utah
- Real Name: Jason Bullard
i like that script, however i think that cookies would be best, or something on the server side. As i have 90 images and counting to rotate. the owner wants the artists portfolios accessed and at random an image to be selected and posted each week.
with that code i would have to add some more stuff in... IE folder dating and it would need to select the year and month that it is, then randomize the images by what was uploaded that week. since they will only be updating the images about once every three moths, this method would not work.
AWESOME find though
with that code i would have to add some more stuff in... IE folder dating and it would need to select the year and month that it is, then randomize the images by what was uploaded that week. since they will only be updating the images about once every three moths, this method would not work.
AWESOME find though
Cookies really isn't likely what you want, they're *very* dependent on the user side, and a lot of users anymore kill cookies by default, either completely or at browser close (meaning the cookie would be lost within your week timeframe). Also, with cookies, the image shown to a given user would differ from one to the next, so any user to user interaction about it would be rather useless, as they could be talking to each other about any of the images and never realize it (people assume consistency).
You could, though, use some server side code (I could write up something to do the job in php fairly easily) to detect what day/week it is and pull up another image. The work of it would all be transparent to the user, who'd see it as though you simply replaced the old weekly feature with the new. If you have anything other than php (but still server side), I'm rather helpless, but may be able to point you in the right direction.
As for doing it only with JS, my best idea is just to set up a pre-written array of image filenames, and have it pull the user's date, subtract an initial date (converted to seconds or days from some given epoch), convert that to weeks from the initial date, and then use that as your array index to pick what image you should be using. Getting it truly "random" but consistent across all users per week ... might be possible, but the how (without server side coding) isn't coming to me. Oh, and the above *is* limited in consistency by the user's date/time being accurate, but that's their fault, at that point, not yours.
EDIT: If you can't do PHP/ASP/etc., but you can set scheduled tasks (or cron jobs) on the server ... you can have a text written weekly with the new random image filename, then have *that* pulled by javascript and written into the document on load ... but hosting that offers you that much control almost always offers some sort of server side coding.
You could, though, use some server side code (I could write up something to do the job in php fairly easily) to detect what day/week it is and pull up another image. The work of it would all be transparent to the user, who'd see it as though you simply replaced the old weekly feature with the new. If you have anything other than php (but still server side), I'm rather helpless, but may be able to point you in the right direction.
As for doing it only with JS, my best idea is just to set up a pre-written array of image filenames, and have it pull the user's date, subtract an initial date (converted to seconds or days from some given epoch), convert that to weeks from the initial date, and then use that as your array index to pick what image you should be using. Getting it truly "random" but consistent across all users per week ... might be possible, but the how (without server side coding) isn't coming to me. Oh, and the above *is* limited in consistency by the user's date/time being accurate, but that's their fault, at that point, not yours.
EDIT: If you can't do PHP/ASP/etc., but you can set scheduled tasks (or cron jobs) on the server ... you can have a text written weekly with the new random image filename, then have *that* pulled by javascript and written into the document on load ... but hosting that offers you that much control almost always offers some sort of server side coding.
- poisonbl
- Posts: 432
- Joined: Mon Nov 15, 2004 10:02 pm
- Location: WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)
Sorry man, I have been so busy lately with other programming stuff. But, I would suggest using PHP as it is a much better solution for what you want. I will see if I can't create a script for you. May take a week but I will try and get something for you. At least something to start with.
- jbullard
- VP - Software
- Posts: 3653
- Joined: Sun Jun 06, 2004 10:17 pm
- Location: Utah
- Real Name: Jason Bullard
15 posts
• Page 1 of 2 • 1, 2
Return to HTML, CSS, and Scripts
Who is online
Users browsing this forum: No registered users and 7 guests