...

View Full Version : Merging codes



davegreen
12-09-2004, 03:14 PM
Hi,

i was wondering if anyone could help me.

i have two bits of code

<html>
<body>
<script type="text/javascript">


number1 = parseInt(Math.random() * 6+1);
number2 = parseInt(Math.random() * 6+1);
number3 = parseInt(Math.random() * 6+1);
number4 = parseInt(Math.random() * 6+1);

document.write(number1 + "" + number2 + "" + number3 + "" + number4);


document.write('<img src="die'+ number1 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number2 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number3 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number4 +'.bmp">');

</script>
</body>
</html>

This displays 4 random die images i have saved in file

davegreen
12-09-2004, 03:15 PM
an this is the 2nd one


<html>

<body>
<script type="text/javascript">

function nextroll(){
number1 = parseInt(Math.random() * 6+1);
number2 = parseInt(Math.random() * 6+1);
number3 = parseInt(Math.random() * 6+1);
number4 = parseInt(Math.random() * 6+1);

document.Score.display.value+=(number1 + "" + number2 + "" + number3 + "" + number4)+"\n";

}

document.write('<img src="die'+ number1 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number2 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number3 +'.bmp">');
document.write(" ");
document.write('<img src="die'+ number4 +'.bmp">');

</script>

<form name="Score">
<center>
<input type="button" value="roll" onclick=nextroll()>
<input type="reset" value="reset">
<textarea name="display" rows="20" cols="40" readonly></textarea>

</form>

</body>

</html>

davegreen
12-09-2004, 03:16 PM
as you can see in the 2nd one i want a button that on roll displays everything, and puts it into a text box

it does that, but doesnt even display the image

ive even tried making the function cover that action too

Willy Duitt
12-09-2004, 04:29 PM
Your problem is two fold...

Your images are never loading because the variables (number1, number2, ect...) although global are not defined on page load (they are defined when nextroll is called) when the document.write statements write your image tags to the page... Thus, the very first image looking for the variable number1 throws and error and the script quits right there (turn on error reporting so you can find these yourself)...

And, even if the images loaded, there is nothing in your nextroll function to update the image sources... so naturaly the textarea is updated but the images will not be updated even if they were visible on the page....

Suggestion: Place the images in the body of the document using names or ids along with default image sources and place your next roll in the head and change the image and either call the function window.onload or body onload changing the image sources whenever the function fires...

Is this homework? Seems so...

.....Willy

jbot
12-09-2004, 04:34 PM
Is this homework? Seems so.

could be.
see here for details (http://www.webdeveloper.com/forum/showthread.php?s=&threadid=51060) :p

Willy Duitt
12-09-2004, 05:15 PM
could be.
see here for details (http://www.webdeveloper.com/forum/showthread.php?s=&threadid=51060) :p

Hmmmmm;

Multiple online personalities...
I suppose I should add both: jonnyheald & davegreen
To my do not write code for list....

Cheers and thanks for the heads up;
.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum