simple script that allows random images to appear | | |
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 | | | | re: simple script that allows random images to appear
Brian McCabe wrote on 20 jan 2006 in comp.lang.javascript:
[color=blue]
> 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?[/color]
<http://www.google.com/search?q=javascript+random+pictures>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress) | | | | re: simple script that allows random images to appear
"Brian McCabe" <briansmccabe@gmail.com> wrote in message
news:1137769350.955642.191130@g43g2000cwa.googlegr oups.com...[color=blue]
> 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
>[/color]
a) this isn't an ASP newsgroup.
b) can you post a URL for us?
c) JavaScript may be disabled. | | | | re: simple script that allows random images to appear
Brian McCabe wrote:[color=blue]
> 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[/color]
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 | | | | re: simple script that allows random images to appear
impaler wrote:
[color=blue]
> 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";[/color]
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.
[color=blue]
> [...]
> 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;[/color]
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);
[color=blue]
> document.getElementById("randomHrefA").href = imgArray[nr].href;[/color]
<URL:http://pointedears.de/scripts/test/whatami#inference>
<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html#bdFD>
[color=blue]
> document.getElementById("randomHrefImg").src = imgArray[nr].imgSrc;[/color]
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 | | | | re: simple script that allows random images to appear
JRS: In article <2059671.4c3CiM6s3q@PointedEars.de>, dated Fri, 20 Jan
2006 19:05:50 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <PointedEars@web.de> posted :[color=blue]
> Should be
>
> var nr = Math.floor(Math.random() * imgArray.length);
>[/color]
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. | | | | re: simple script that allows random images to appear
McKirahan wrote:[color=blue]
> "Brian McCabe" <briansmccabe@gmail.com> wrote in message
> news:1137769350.955642.191130@g43g2000cwa.googlegr oups.com...[color=green]
> > 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
> >[/color]
>
> a) this isn't an ASP newsgroup.[/color]
No shit. don't be a dick.[color=blue]
>
> b) can you post a URL for us?[/color]
No, because the page in question was on a development server that
you would not be able to view.[color=blue]
>
> c) JavaScript may be disabled.[/color]
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. | | | | re: simple script that allows random images to appear
Dr John Stockton wrote:
[color=blue]
> [...] Thomas 'PointedEars' Lahn [...] posted :[color=green]
>> Should be
>>
>> var nr = Math.floor(Math.random() * imgArray.length);
>>[/color]
>
> Could, not should.[/color]
Should, not must. Because the algorithm used before was _wrong_.
PointedEars | | | | re: simple script that allows random images to appear
JRS: In article <1480496.Z4TKIdPkjQ@PointedEars.de>, dated Mon, 23 Jan
2006 15:00:44 remote, seen in news:comp.lang.javascript, Thomas
'PointedEars' Lahn <PointedEars@web.de> posted :[color=blue]
>Dr John Stockton wrote:
>[color=green]
>> [...] Thomas 'PointedEars' Lahn [...] posted :[color=darkred]
>>> Should be
>>>
>>> var nr = Math.floor(Math.random() * imgArray.length);
>>>[/color]
>>
>> Could, not should.[/color]
>
>Should, not must. Because the algorithm used before was _wrong_.[/color]
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. |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|