Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   HTML (http://www.velocityreviews.com/forums/f31-html.html)
-   -   random picture (http://www.velocityreviews.com/forums/t958936-random-picture.html)

meagain 03-22-2013 12:42 AM

random picture
 
I've used javascript to generate a random number,
but can you tell me how to choose a random "img"?

--
www.BoltonAccess.TV

Chris F.A. Johnson 03-22-2013 01:19 AM

Re: random picture
 
On 2013-03-22, meagain wrote:
> I've used javascript to generate a random number,
> but can you tell me how to choose a random "img"?


I use this CGI script: <http://b.cfaj.ca/rpic.cgi>

#!/bin/bash
name=rpic.cgi
version=1.0
description=
author='Chris F.A. Johnson'
created='2013-03-14 14:34:55'
modified=2013-03-21T21:15:42

printf 'Content-type: image/jpeg\n\n'

list=( *.jpg )

cat "${list[RANDOM % ${#list[@]}]}"

--
Chris F.A. Johnson
<http://torontowebdesign.cfaj.ca/>

123Jim 03-22-2013 01:40 AM

Re: random picture
 
On 22/03/2013 00:42, meagain wrote:
> I've used javascript to generate a random number,
> but can you tell me how to choose a random "img"?
>


This looks quite good:
http://www.dyn-web.com/code/basics/r...dom_img_js.php

Not tested by myself however.

richard 03-22-2013 03:37 PM

Re: random picture
 
On Thu, 21 Mar 2013 20:42:52 -0400, meagain wrote:

> I've used javascript to generate a random number,
> but can you tell me how to choose a random "img"?


fairly simple to do in PHP.
create an array with img names.
then use the random function to select one.
show the img.

Tim W 03-23-2013 12:33 PM

Re: random picture
 
On 22/03/2013 15:37, richard wrote:
> On Thu, 21 Mar 2013 20:42:52 -0400, meagain wrote:
>
>> I've used javascript to generate a random number,
>> but can you tell me how to choose a random "img"?

>
> fairly simple to do in PHP.
> create an array with img names.
> then use the random function to select one.
> show the img.
>


Or get a random image script off the internet like this one I have used
http://www.phpjunkyard.com/random-image.php

meagain 03-24-2013 11:58 PM

Re: random picture
 
123Jim wrote:
> On 22/03/2013 00:42, meagain wrote:
>> I've used javascript to generate a random number,
>> but can you tell me how to choose a random "img"?
>>

>
> This looks quite good: http://www.dyn-web.com/code/basics/r...dom_img_js.php
>
> Not tested by myself however.



Thanks to all respondents for their tips and hints!




Denis McMahon 04-02-2013 04:52 AM

Re: random picture
 
On Thu, 21 Mar 2013 20:42:52 -0400, meagain wrote:

> I've used javascript to generate a random number,
> but can you tell me how to choose a random "img"?


The following is one way to do so in javascript, as you said you've used
javascript, but I actually have no idea whether you want to use javascript
for this or not .....

You can load an array of n image locations into a document thus:

<script type="text/javascript">
var images = new Array();
images.push("http://host1.domain1.tld1/path1/imagefile1.ext1");
images.push("http://host2.domain2.tld2/path2/imagefile2.ext2");
images.push("http://host3.domain3.tld3/path3/imagefile3.ext3");
........
images.push("http://hostn.domainn.tldn/pathn/imagefilen.extn");
</script>

You can define an image in html thus:

<img id="randomImage">

or in xhtml thus:

<img id="randomImage1">

And then you can load a random image from your array of images with:

<script type="text/javascript">
var index = floor(random()*images.count);
document.getElementById("randomImage1").src = images[index];
</script>

It may be better to put the latter javascript in a function called from
the onload event of the document body:

<html>
<head>
<title>random image</title>
<script type="text/javascript">
var images = new Array();
images.push("http://www.sined.co.uk/tmp/sq-1-3.jpg");
images.push("http://www.sined.co.uk/tmp/sq-20-27.jpg");
images.push("http://www.sined.co.uk/tmp/sq-28-35.jpg");
images.push("http://www.sined.co.uk/tmp/sq-36-39.jpg");
images.push("http://www.sined.co.uk/tmp/sq-40.jpg");
images.push("http://www.sined.co.uk/tmp/sq-4-19.jpg");
images.push("http://www.sined.co.uk/tmp/square_base.jpg");
function pageLoaded() {
var index = Math.floor(Math.random()*images.length);
document.getElementById("randomImage1").src = images[index];
}
</script>
</head>
<body onload="pageLoaded();">
<p><img id="randomImage1"></p>
</body>
</html>

This "Random Image with Javascript" web page can be seen at http://
www.sined.co.uk/tmp/randomimg.htm

--
Denis McMahon, denismfmcmahon@gmail.com

Denis McMahon 04-02-2013 07:50 AM

Re: random picture
 
On Tue, 02 Apr 2013 04:52:51 +0000, Denis McMahon wrote:

> or in xhtml thus:
> <img id="randomImage1"> <---- this


Should have been this:

<img id="randomImage1" />

--
Denis McMahon, denismfmcmahon@gmail.com

Jan Clemens Faerber 04-04-2013 03:11 PM

Re: random picture
 
On Friday, March 22, 2013 1:42:52 AM UTC+1, meagain wrote:
> I've used javascript to generate a random number,
>
> but can you tell me how to choose a random "img"?


Which random number?
A random number between 1 and 20
or a random number between 0 and 1?
There are many possible ranges of numbers.


Jan Clemens Faerber 04-04-2013 05:02 PM

Re: random picture
 
On Tuesday, April 2, 2013 6:52:51 AM UTC+2, Denis McMahon wrote:
> On Thu, 21 Mar 2013 20:42:52 -0400, meagain wrote:
> > I've used javascript to generate a random number,
> > but can you tell me how to choose a random "img"?

>
> The following is one way to do so in javascript, as you said you've used
> javascript, but I actually have no idea whether you want to use javascript
> for this or not .....


your javascript example looks well -
but isn't it much more easy (in case
there is a relation:
"random number"~"number of pics")
to use document.writeln(... and here
you use the random number to write
the number of the gif (.jpg, whatever)?


All times are GMT. The time now is 05:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.