Click to See Complete Forum and Search --> : Random number generator/form


Doric
09-26-2003, 08:09 AM
Hello, everyone.

I'm an IT admin with little experience in web development, outside of HTML and a few small java applets. I need, however, to create a form that does the following: On input, a random number is generated, The number is associated to a block of text in TWO parts (i.e., question & answer), The resulting FIRST part is displayed on the screen, and a link to the second part of the result is displayed, maybe even as a pop-up....Or vice versa.

Can anyone help me with that? I can code the page just fine. And maybe the random number generator isn't needed, but I have about 125 questions and answers I need to access randomly.

I greatly appreciate any help you can provide.

Thanks.

Doric
09-26-2003, 10:10 AM
Can this code be changed easily to load text instead of images?
<!-- THREE STEPS TO INSTALL RANDOM CLICK IMAGE:

1. Copy the coding into the HEAD of your HTML document
2. Add the onLoad event handler into the BODY tag
3. Put the last coding into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Nicholas Lupien (smylex@aol.com) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var rand1 = 0;
var useRand = 0;

images = new Array;
images[1] = new Image();
images[1].src = "image1.gif";
images[2] = new Image();
images[2].src = "image2.gif";
images[3] = new Image();
images[3].src = "image3.gif";
images[4] = new Image();
images[4].src = "image4.gif";
images[5] = new Image();
images[5].src = "image5.gif";

function swapPic() {
var imgnum = images.length - 1;
do {
var randnum = Math.random();
rand1 = Math.round((imgnum - 1) * randnum) + 1;
} while (rand1 == useRand);
useRand = rand1;
document.randimg.src = images[useRand].src;
}
// End -->
</script>
</HEAD>

<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->

<BODY OnLoad="swapPic()">

<!-- STEP THREE: Copy this code into the BODY of your HTML document -->

<center>
<a onClick="swapPic();"><img name="randimg" src="image1.gif"></a><br>
<font face="Verdana" size="-2">click image to change</font>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.49 KB -->

Khalid Ali
09-26-2003, 10:28 AM
Something like this might help

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
<!-- Begin
var rand1 = 0;
var useRand = 0;

var textArr = new Array;
textArr[0] = "First text string 0";
textArr[1] = "Another string 1";
textArr[2] = "heck yet another string 2";

function changeText() {
var imgnum = textArr.length - 1;
do {
var randnum = Math.random();
rand1 = Math.round((imgnum) * randnum) ;
} while (rand1 == useRand);
useRand = rand1;
document.getElementById("div_text").innerHTML = textArr[useRand];
}
// End -->
</script>
<title>
</title>
</head>
<body onload="changeText()">
<center>
<a onclick="changeText();">Cahnge Text</a><br/>
<font face="Verdana" size="-2">click image to change</font><br/><br/>
<div id="div_text"></div>
</center>
</body>
</html>

Doric
09-26-2003, 10:42 AM
Thanks! That does help.

Now, not to be a nuisance or anything, but how can I get the output to display three separate text strings at once, and can I make each text string a link to a pop-up displaying another text string?

Khalid Ali
09-26-2003, 11:04 AM
you are welcome..

well take a look at this part of the code

textArr[0] = "First text string 0";

what you can do is add anything to it like

textArr[0] = "First text string 0<br/>second line<br/><a href='poupurl.html' onclick='window,open(\"poupurl.html\",\"\",\"width=100,height=100\")'>Click to open Popup</a>";

the above example adds multiple lines to the contents as well as a link that will open a popup window.

Doric
09-29-2003, 01:01 PM
Thanks - a lot - for your help. Slowly but surely, I'm learning, and developing the page that I need.

I cribbed a timer from the archives:
var up,down;var min1,sec1;var cmin1,csec1,cmin2,csec2;
function Minutes(data) {
for(var i=0;i<data.length;i++)
if(data.substring(i,i+1)==":")
break;
return(data.substring(0,i));
}
function Seconds(data) {
for(var i=0;i<data.length;i++)
if(data.substring(i,i+1)==":")
break;
return(data.substring(i+1,data.length));
}
function Display(min,sec) {
var disp;
if(min<=9) disp=" 0";
else disp=" ";
disp+=min+":";
if(sec<=9) disp+="0"+sec;
else disp+=sec;
return(disp);
}
function Down() {
cmin2=1*Minutes(document.sw.beg2.value);
csec2=0+Seconds(document.sw.beg2.value);
DownRepeat();
}
function DownRepeat() {
csec2--;
if(csec2==-1) {
csec2=59; cmin2--;
}
document.sw.disp2.value=Display(cmin2,csec2);
if((cmin2==0)&&(csec2==0))
alert("I'm sorry. Your time is up.");
else down=setTimeout("DownRepeat()",1000);
}
It functions alright, except that it speeds up when you click the Start button while it's already running. That's a nuisance, but not a serious problem. What I'd like, however, is a stop function for this timer. It seems to me that it'd be as simple as adding a 'break' function - but I don't know how to code it.

Again, any help is greatly appreciated.

Khalid Ali
09-29-2003, 01:38 PM
this line
down=setTimeout("DownRepeat()",1000);

is running a timer,what you need to do is disable the reference,which in this case is downsetTimeou.....

so what you need to do is write this line of code anywhere wfrom where you want to to stop the timer

clearTimeout(down);

and make sure the [b[down[/b]
is declared like below at the very top of the document

var down;

Doric
09-29-2003, 02:26 PM
I feel completely stupid, but I'm having a hard time understanding what you mean.

Doric
09-29-2003, 02:33 PM
Welp. I got it. Was able to put a Stop button in, simply by creating a new function with just clearTimeout(down);.

Thank you very, very much for your help.

Khalid Ali
09-29-2003, 04:30 PM
:D
u r welcome