/    Sign up×
Community /Pin to ProfileBookmark

Random Start ?

I use this simple code in the body of my site to rotate banners. It’s simple and works well.

How would I modify the code to start the rotation at random (not always the first banner)

Thanks
Sandy

<script type=”text/javascript”>
var imgs1 = new Array(“Temp01a.jpg”,”Temp02a.jpg”,”Temp03a.jpg”,”Temp04a.jpg”,”Temp05a.jpg”,”Temp06a.jpg”,”Temp07a.jpg”,”Temp08a.jpg”,”Temp09a.jpg”);
var lnks1 = new Array(“http://xx1.com”,”http://xx2.com”,”http://xx3.com”,”http://xx4.com”,”http://xx1.com”,”http://xx5.com”,”http://xx5.com”,”http://xx7.com”,”http://xx8.com”,”http://xx9.com””);
var alt1 = new Array();
var currentAd1 = 0;
var imgCt1 = 9;
function cycle1() {
if (currentAd1 == imgCt1) {
currentAd1 = 0;
}
var banner1 = document.getElementById(‘adBanner1’);
var link1 = document.getElementById(‘adLink1’);
banner1.src=imgs1[currentAd1]
banner1.alt=alt1[currentAd1]
document.getElementById(‘adLink1’).href=lnks1[currentAd1]
currentAd1++;
}
window.setInterval(“cycle1()”,6000);
</script>
<P ALIGN=CENTER>
<a href=””http://xx1.com“” id=”adLink1″ target=”_top”>
<img src=”Temp01.jpg” id=”adBanner1″ border=”0″ width=”774″ height=”230″>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 01.2010 — While I'm a little confused as to why your 'imgs1' array length

does not match your 'lnks1' array length as they both appear to need

to use the 'currentAd1' variable ... (Might want to check this for errors)

Try...
<i>
</i>var currentAd1 = Math.floor(Math.random() * lnks1.length);
Copy linkTweet thisAlerts:
@motuitiauthorFeb 01.2010 — Not sure what you mean. Both Image and link array are the same (9) in the sample.

And as far as your suggestion.

I replaced

var currentAd1 = 0;

With Your suggestion

var currentAd1 = Math.floor(Math.random() * lnks1.length);

there was no change. Still starts with first image.
Copy linkTweet thisAlerts:
@JMRKERFeb 02.2010 — Might want to count the array sizes again in your original post

(I count 9 and 10. I added one to 'imgs1' to make it 10 and 10)

You also cannot use ""xxx"" in a link (double double quote)

You have not initialized 'alt1' to anything

You did not have an ending anchor </a> tag

Several other errors scattered about.

Try this:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Random Imager&lt;/title&gt;

&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1064867#post1064867

var imgs1 = new Array(
"Temp01a.jpg","Temp02a.jpg","Temp03a.jpg","Temp04a.jpg","Temp05a.jpg",
"Temp06a.jpg","Temp07a.jpg","Temp08a.jpg","Temp09a.jpg","Temp01.jpg" // added one extra
);
var lnks1 = new Array(
"http://xx1.com","http://xx2.com","http://xx3.com","http://xx4.com","http://xx1.com",
"http://xx5.com","http://xx5.com","http://xx7.com","http://xx8.com","http://xx9.com"
);

var alt1 = new Array(); // NOT initialized to anything

var currentAd1 = Math.floor(Math.random() * lnks1.length);

function cycle1() {
if (currentAd1 == lnks1.length) { currentAd1 = 0; }
var banner1 = document.getElementById('adBanner1');
var link1 = document.getElementById('adLink1');
banner1.src=imgs1[currentAd1];

banner1.alt=imgs1[currentAd1]; // temporary replacement for next line
// banner1.alt=alt1[currentAd1]; // NOT initialized

document.getElementById('adLink1').href=lnks1[currentAd1];
currentAd1++;
}

function InitializeBanner() {
var tmp = setInterval("cycle1()",2000);
}

&lt;/script&gt;

&lt;body onload="InitializeBanner()"&gt;

&lt;div style="text-align:center"&gt;
&lt;a href="http://xx1.com" id="adLink1" target="_top"&gt;
&lt;img src="" alt="" id="adBanner1" border="0" width="774" height="230"&gt;
&lt;/a&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

Be sure to 'reload' several times as having only 10 images is not as much random as having 100.

Good Luck!

?
×

Success!

Help @motuiti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...