Solved

Random

Posted on 1998-10-28
7
266 Views
Last Modified: 2010-04-09
I want to be able to use a javascript to display a random image out of a set of 4 on my web page. Anyone know the code for this?
0
Comment
Question by:Bishma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Expert Comment

by:MasseyM
ID: 1839209
Here you go:

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
numpics = 4
day = new Date()
z = day.getTime()
y = (z - (parseInt(z/1000,10) * 1000))/10
x = parseInt(y/100*nummidi,10) + 1
if (x == (1))
pic=("pic1.gif")
if (x == (2))
pic=("pic2.gif")
if (x == (3))
pic=("pic3.gif")
if (x == (4))
pic=("pic4.gif")
document.write('<IMG SRC= ' + pic + ' >')
// End -->
</SCRIPT>

0
 
LVL 4

Expert Comment

by:martinag
ID: 1839210
If your images are named "pic1.gif" to "pic4.gif" this:
if (x == (1))
pic=("pic1.gif")
if (x == (2))
pic=("pic2.gif")
if (x == (3))
pic=("pic3.gif")
if (x == (4))
pic=("pic4.gif")
Can be replace with this:
pic = "pic" + x + ".gif";

Martin
0
 

Author Comment

by:Bishma
ID: 1839211
It didn't work. Is there a line that should be in the body to accompany this? Keep in mind I know very little about javascript
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
Grdv earned 60 total points
ID: 1839212
<html>
<head>
<script language="javascript">
function rand(n){
 var now = new Date();
 var seed = now.getTime() % 0xffffffff;
 seed = (0x015a4e35 * seed) % 0x7fffffff;
 return (seed >> 16) % n;
}

function splash(){
 if (document.images) document.images["splash"].src = "img/splash" + rand(4) + ".gif";
}
</script>
<title>Splash Screen Page</title>
</head>
<body bgcolor="#cccc66" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" onLoad="splash()">
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
 <tr>
  <td align="center" valign="middle" colspan="2">
    <a href="about.html">
<img src="img/bl.gif" alt="splashpicture" width="370" height="370" border="0" name="splash"></a>
  </td>
 </tr>
</table>
</body>
</html>
this is very simple... all you do is to place your pictures in a subdir, called img and then name them:
splash0.gif,splash1.gif,splash2.gif,splash3.gif

and then this image tag is the keytag:
<img src="img/bl.gif" alt="splashpicture" width="370" height="370" border="0" name="splash">
first of all the src= points to a blank image, just to have an image then the name "splash" is the one used of the javascript function splash()

if (document.images) document.images["splash"].src = "img/splash" + rand(4) + ".gif";
checks if there is possible to change the image, if it is, then change the image named "splash" into the random splash screen...

hope that helped...

//Grdv
0
 

Author Comment

by:Bishma
ID: 1839213
Not exactly what I wanted but it only took 2 small changes to get it to work. Thanks.
0
 
LVL 10

Expert Comment

by:MasseyM
ID: 1839214
That is exactly what mine did!
0
 
LVL 2

Expert Comment

by:Grdv
ID: 1839215
no it's not.... you're using this line:

      document.write('<IMG SRC= ' + pic + ' >')

while I'm using dynamic images, then which one of them's best can be discussed...

//Grdv
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide file extensions in Notepad++ tabs 2 79
javascript works in Chrome, but none of the other browsers 14 54
Check input text, Number 7 54
Insert PHP into HTML page. 7 45
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question