random picture

M

meagain

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

Chris F.A. Johnson

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[@]}]}"
 
R

richard

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.
 
D

Denis McMahon

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
 
J

Jan Clemens Faerber

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.
 
J

Jan Clemens Faerber

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)?
 
D

Denis McMahon

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)?

Yes, you can do it that way, but working through the DOM seems to be
generally considered as better practice than document.write/ln, and
provides a method that can be used to, for example, change the random
image to another one when it is clicked on. I'm not sure that's so easy
to do with document.write/ln

http://www.sined.co.uk/tmp/randomimg2.htm

Note that sometimes it will choose the current image as the new random
image, so if it doesn't change when you click it, just click it again.
 
J

Jan Clemens Faerber

Yes, you can do it that way, but working through the DOM seems to be
generally considered as better practice than document.write/ln, and
provides a method that can be used to, for example, change the random
image to another one when it is clicked on. I'm not sure that's so easy
to do with document.write/ln

http://www.sined.co.uk/tmp/randomimg2.htm

Note that sometimes it will choose the current image as the new random
image, so if it doesn't change when you click it, just click it again.

I understand.
Last time I even saw a way to switch the css style sheet with a few lines js using a cookie which is written again after a reload :)

For more css reload check out maybe
https://github.com/auchenberg/css-reloader
It's currently available as addon/extension
for Mozilla Firefox and Google Chrome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top