By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

simple script that allows random images to appear

P: n/a
hello -

I am very new to javascript and have a pretty tight deadline (as in,
today!) in getting a new version of a page released. The built-in ASP
feature that allows for any one of a series of images to randomly
appear upon page refresh is not cooperating at all. Can someone point
me in the direction of a script that will accomplish this? Basically
the page (among other things) has one image at the top that links to
some other section of the site, and each time the page refreshes a
different image appears with a different href.

Thanks -

Brian mccabe

Jan 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Brian McCabe wrote on 20 jan 2006 in comp.lang.javascript:
I am very new to javascript and have a pretty tight deadline (as in,
today!) in getting a new version of a page released. The built-in ASP
feature that allows for any one of a series of images to randomly
appear upon page refresh is not cooperating at all. Can someone point
me in the direction of a script that will accomplish this?


<http://www.google.com/search?q=javascript+random+pictures>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #2

P: n/a
"Brian McCabe" <br**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hello -

I am very new to javascript and have a pretty tight deadline (as in,
today!) in getting a new version of a page released. The built-in ASP
feature that allows for any one of a series of images to randomly
appear upon page refresh is not cooperating at all. Can someone point
me in the direction of a script that will accomplish this? Basically
the page (among other things) has one image at the top that links to
some other section of the site, and each time the page refreshes a
different image appears with a different href.

Thanks -

Brian mccabe


a) this isn't an ASP newsgroup.

b) can you post a URL for us?

c) JavaScript may be disabled.
Jan 20 '06 #3

P: n/a

Brian McCabe wrote:
hello -

I am very new to javascript and have a pretty tight deadline (as in,
today!) in getting a new version of a page released. The built-in ASP
feature that allows for any one of a series of images to randomly
appear upon page refresh is not cooperating at all. Can someone point
me in the direction of a script that will accomplish this? Basically
the page (among other things) has one image at the top that links to
some other section of the site, and each time the page refreshes a
different image appears with a different href.

Thanks -

Brian mccabe


I can give you guidelines, cause I'm short on time too sorry to write
accurate code :(

Build an array of images and hrefs (if it's fixed put it in javascript
if not generate it from ASP)

var imgArray = new Array;
imgArray[0].imgSrc = "src to image1";
imgArray[0].href = "href of image1";
imgArray[1].imgSrc = "src to image2";
imgArray[1].href = "href of image2";
....

have the href object created on the page or add it dynamically using
DOM.

1 assume it's created like this:
<a id="randomHrefA" href="#"><img id="randomHrefImg"src=""/></a>

and on the body onload call a function:

function putRandomLink() {
var nr = Math.random() * imgArray.length;
document.getElementById("randomHrefA").href = imgArray[nr].href;
document.getElementById("randomHrefImg").src = imgArray[nr].imgSrc;
}

add this to the <body> tag
<body onload="putRandomLink()">

finally I provided some (untested) code too :)

this will pick a random image with its href and put it in the link.

good luck

Jan 20 '06 #4

P: n/a
impaler wrote:
var imgArray = new Array;
imgArray[0].imgSrc = "src to image1";
imgArray[0].href = "href of image1";
imgArray[1].imgSrc = "src to image2";
imgArray[1].href = "href of image2";
var imgArray = [
{imgSrc: "src to image1", href: "href of image1"},
{imgSrc: "src to image2", href: "href of image2"}
];

is (downwards) compatible to JavaScript 1.3, JScript 2.0, ECMAScript 3.
[...]
have the href object created on the page or add it dynamically using
DOM.

1 assume it's created like this:
<a id="randomHrefA" href="#"><img id="randomHrefImg"src=""/></a>

and on the body onload call a function:

function putRandomLink() {
var nr = Math.random() * imgArray.length;
With a high statistical probability, this will evaluate to a number with a
non-zero fractional part. Math.random() returns an IEEE-754 floating point
number in the right-hand side open interval [0, 1) and therefore not refer
to an element of the array at all. Should be

var nr = Math.floor(Math.random() * imgArray.length);
document.getElementById("randomHrefA").href = imgArray[nr].href;
<URL:http://pointedears.de/scripts/test/whatami#inference>
<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html#bdFD>
document.getElementById("randomHrefImg").src = imgArray[nr].imgSrc;


document.images['randomHrefImg'].src = imgArray[nr].imgSrc;

It would be downwards compatible with using the `name' attribute for the
`img' element instead of the `id' attribute.
PointedEars
Jan 20 '06 #5

P: n/a
JRS: In article <20****************@PointedEars.de>, dated Fri, 20 Jan
2006 19:05:50 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Should be

var nr = Math.floor(Math.random() * imgArray.length);


Could, not should. For any likely number of images, one can use

var nr = (Math.random() * imgArray.length) | 0 ;

For reliable compatibility with certain defective browsers, use
Math.random()%1 .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 21 '06 #6

P: n/a

McKirahan wrote:
"Brian McCabe" <br**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hello -

I am very new to javascript and have a pretty tight deadline (as in,
today!) in getting a new version of a page released. The built-in ASP
feature that allows for any one of a series of images to randomly
appear upon page refresh is not cooperating at all. Can someone point
me in the direction of a script that will accomplish this? Basically
the page (among other things) has one image at the top that links to
some other section of the site, and each time the page refreshes a
different image appears with a different href.

Thanks -

Brian mccabe

a) this isn't an ASP newsgroup.


No shit. don't be a dick.
b) can you post a URL for us?
No, because the page in question was on a development server that
you would not be able to view.
c) JavaScript may be disabled.


What the hell is your point?

Learn to utilize the magic of inference. Everyone else that replied
was able to determine that I was seeking a javascript script, not an
asp script.

Jan 22 '06 #7

P: n/a
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Should be

var nr = Math.floor(Math.random() * imgArray.length);


Could, not should.


Should, not must. Because the algorithm used before was _wrong_.
PointedEars
Jan 23 '06 #8

P: n/a
JRS: In article <14****************@PointedEars.de>, dated Mon, 23 Jan
2006 15:00:44 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <Po*********@web.de> posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
Should be

var nr = Math.floor(Math.random() * imgArray.length);


Could, not should.


Should, not must. Because the algorithm used before was _wrong_.


Well, Pooh, you are right in saying that the algorithm used before was
wrong. It must be corrected. But you are not right in saying that one
should correct it as you suggested, since there is other code that will
do the job (FWIW, shorter and quicker than yours). Try understanding
what you respond to before you respond. Indeed, try understanding what
you yourself have written.

And try to control your inclination to childish bullying. Remember,
when the general opinion expressed is that you have some technical skill
but are obnoxious in manner, then it's reasonable to assume that you are
indeed obnoxious and then discover how to correct it.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.