...

View Full Version : Script to Diplay random images.



kobyskid
01-19-2009, 08:37 PM
I am stuck on some javascript homework I realize it is probably really easy but my stupid textbook is not clear on one point. I have to access code in an external javascript page (random.js) which I have done. But then i have to create a second script element to create the function showlmg() and within that I require statements to declare a variable named imgNumber equal to the value returned by the randomInteger() (which is from the external sheet found in data files from the textbook therefore I know they are right). I am supposed to use 9 as the value of the size parameter in the randomInteger() function. The second statement should be command that writes the following text to the document: <img src='imgNumber.jog' alt= ' '/>. So basically the coding I have done is :



<script type= "text/javascript" src= random.js"> </script>
<script type="text/javascript">
function showlmg() {

var imgNumber= randomInteger(9);// Return a random number from 0 to 9.
document.write("<img src='imgNumber.jpg' alt=''/>");
}
</script>

all in the head of the document and then


function randomInteger(size) {
return Math.floor((size+1)*Math.random());} from the external file.

and



<tr>
<td colspan="2" class="center">
<script type= "text/javascript">
showlmg();
showlmg();
showlmg();
showlmg();
showlmg();
</script>
</td>
</tr>

to post in a table on the page... I believe the issue is in the declaring a variable the is equal to the result of the function random integer which the text book or anywhere that I can find online fails to explain how to do. Thank you.

ninnypants
01-19-2009, 09:53 PM
In JavaScript you always have to break your variables out of strings so you should have

document.write("<img src='"+imgNumber+".jpg' alt=''/>");

instead of

document.write("<img src='imgNumber.jpg' alt=''/>");

kobyskid
01-19-2009, 10:05 PM
Did that but the random images still don't appear

jmrker
01-20-2009, 05:08 AM
What are the names of your image file?

1.jpg to 9.jpg
or
imgNumber1.jpg to imgNumber9.jpg

Whatever their names, are they located in the same directory as the script?

Are you getting any errors in the error console?
Do you have a link to a test site?

kobyskid
01-20-2009, 09:30 PM
image names are 1.jpg to 9.jpg no test link but they are in the same folder

jmrker
01-21-2009, 02:15 AM
The following works in FF and IE as is.
Check it out to be sure, then remove the 3 lines to the external 'random.js' file
and uncomment the external script call. All should work as before


<html>
<head>
<title>Random Images</title>
<!-- script type= "text/javascript" src= random.js"> </script -->
<script type="text/javascript">
function randomInteger(size) {
return Math.floor((size+1)*Math.random()); // from the external file.
}
function showlmg() {
var imgNumber= randomInteger(9);// Return a random number from 0 to 9.
document.write("<img src='"+imgNumber+".jpg' alt='"+imgNumber+".jpg' height='100' width='100' />");
}

// From: http://www.codingforums.com/showthread.php?p=772337#post772337
</script>
</head>
<body>
<h1>Random Images</h1>
<table>
<tr>
<td colspan="2" class="center">
<script type= "text/javascript">
showlmg();
showlmg();
showlmg();
showlmg();
showlmg();
</script>
</td>
</tr>
</table>
</body>
</html>

Make sure you do not have any HTML code in the external JS file.
When all works, go back and check the differences line by line in your original script.

Good Luck!
:)

Ramvicious
10-21-2014, 09:35 PM
What are the names of your image file?

1.jpg to 9.jpg
or
imgNumber1.jpg to imgNumber9.jpg

Whatever their names, are they located in the same directory as the script?

Are you getting any errors in the error console?
Do you have a link to a test site?


I know I am necroing this thread, but as this topic is still relevant. ( I came here with the same problem) I figured I could answer this to help out future people. The reason the images aren't showing is because the imgs are not .jpg but .png images. I was having the same problem. It's an easy fix but also very easy to overlook.

jmrker
10-22-2014, 02:59 AM
I know I am necroing this thread, but as this topic is still relevant. ( I came here with the same problem) I figured I could answer this to help out future people. The reason the images aren't showing is because the imgs are not .jpg but .png images. I was having the same problem. It's an easy fix but also very easy to overlook.

Thanks for resolving the problem.
I was trusting that the OP knew the names of his own files :rolleyes:
but he/she never responded to any questions. :confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum