...

View Full Version : Help, should be easy



fus10n
12-10-2010, 12:27 AM
hey guys im trying to make something that selects two different images but it keeps selecting two images that are the same, and dont know how to go about it to make it select two different. please help


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/javascript; charset=utf-8" />
<title>Untitled Document</title>
<script language="JavaScript">
<!--
var theImages = new Array()

theImages[0] = '100.jpg'
theImages[1] = '200.JPG'
theImages[2] = '300.JPG'
theImages[3] = '400.jpg'


var j = 0
var p = theImages.length;

var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}

var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//-->
</script>


</head>

<body>



<div align="center"></div>
<table width="996" height="511" border="1">
<tr>
<th scope="col"><script language="JavaScript">
<!--

showImage();
//-->
</script>
&nbsp; or
<script language="JavaScript">
<!--

showImage();
//-->
</script> </th>
</tr>
</table>
</body>
</html>

Old Pedant
12-10-2010, 12:40 AM
<script type="text/javascript">
var theImages = ["100.jpg","200.jpg","300.jpg","400.jpg"];
theImages.sort( new function() { return Math.random() - 0.5; } );

var curImage = 0;
function showImage()
{
var show = curImage % theImages.length;
++curImage;
document.write('<img src="'+theImages[show]+'">');
}
</script>

(a) There is no reason to preload the images when you are going to document.write <img> tags, anyway. You only preload when you will be changing images *after* the page is loaded.
(b) The use of <!-- and --> became unnecessary when MSIE 3 died an overdue death....back in 1998 or so.
(c) The use of language="..." is almost as out of date. Use type="..." instead, as shown.

fus10n
12-10-2010, 12:45 AM
ok, i replaced the header script with that and now it isnt displaying any images, sorry im a newbie

jmrker
12-10-2010, 01:00 AM
Here's my attempt at changing your code. Old Pedant's may be shorter, but I tried to use as much of your code as possible...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/javascript; charset=windows-1252" />
<title>Untitled Document</title>
<script type="text/javascript"> <!-- archaic form: language="JavaScript" -->

var baseURL = 'http://www.nova.edu/hpd/otm/pics/4fun/'; // path to image directory
var theImages = ['11.jpg','21.jpg','31.jpg','41.jpg'];
var preBuffer = [];

for (var i=0; i<theImages.length; i++) {
preBuffer[i] = new Image();
preBuffer[i].src = theImages[i];
}

function randOrd() {
return (Math.round(Math.random())-0.5);
}

function showImage(){
theImages = theImages.sort(randOrd);
document.write('<img src="'+baseURL+theImages[0]+'">');
document.write('&nbsp; or ');
document.write('<img src="'+baseURL+theImages[1]+'">');
}

</script>
</head>
<body>

<div align="center"></div>
<table width="996" height="511" border="1">
<tr>
<th scope="col"><script type="text/javascript"> showImage(); </script></th>
</tr>
</table>
</body>
</html>

Change the 'baseURL' and image names to your files.

Good Luck!
:)

fus10n
12-10-2010, 01:10 AM
Thank you so much!

jmrker
12-10-2010, 03:41 AM
Thank you so much!

Your most welcome.
I'm glad we could help (whatever code you used).
Good Luck!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum