A Digital Age Deserves A Digital Leader

Image rotation with javascript help needed

Image rotation with javascript help needed

Postby c0ldfyr3 » Sat May 17, 2008 8:17 am

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.
Image
PRO Level 15
User avatar
Posts: 1346
Joined: Sun May 02, 2004 8:49 pm
Location: 127.0.0.1

Postby jbullard » Sat May 17, 2008 9:36 am

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. :yesnod:
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Postby c0ldfyr3 » Sun May 18, 2008 7:29 pm

awesome... cuz i have no idea how to write a valid cookie....

i have seen a few examples, i am just not so good at it.
Image
PRO Level 15
User avatar
Posts: 1346
Joined: Sun May 02, 2004 8:49 pm
Location: 127.0.0.1

Postby jbullard » Thu May 22, 2008 2:54 am

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
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Postby c0ldfyr3 » Thu May 22, 2008 4:51 am

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 ^*^
Image
PRO Level 15
User avatar
Posts: 1346
Joined: Sun May 02, 2004 8:49 pm
Location: 127.0.0.1

Postby jbullard » Thu May 22, 2008 4:20 pm

Do you have other scripting languages availables like PHP?
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Postby poisonbl » Thu May 22, 2008 4:45 pm

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.
Image
Image
PRO Level 10
User avatar
Posts: 432
Joined: Mon Nov 15, 2004 10:02 pm
Location: WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)

Postby c0ldfyr3 » Fri May 23, 2008 12:09 am

i can do php, and that was my original preferred code.
Image
PRO Level 15
User avatar
Posts: 1346
Joined: Sun May 02, 2004 8:49 pm
Location: 127.0.0.1

Postby c0ldfyr3 » Tue Jun 03, 2008 12:16 am

can anyone help with a cookie and how to implement it into the code?
Image
PRO Level 15
User avatar
Posts: 1346
Joined: Sun May 02, 2004 8:49 pm
Location: 127.0.0.1

Postby jbullard » Tue Jun 03, 2008 3:33 am

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. :yesnod:
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Next

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 5 guests