Simple Loop - for Double Loop Conditional Loop - while Example - mosaic |
Start of website Alphabetical index |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Multiplication table function timtab() { var prod = document.times.mult.value var outp = "" for (i = 1; i <= 12; i++) { ans = i * prod outp = outp + i + " x " + prod + " = " + ans + "<BR>" } document.getElementById('outtab').innerHTML = outp } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME=times> Enter number <INPUT TYPE=TEXT NAME=mult SIZE=4> <INPUT TYPE=BUTTON VALUE="Generate table" ONCLICK="timtab()"> </FORM> <BR><SPAN ID="outtab"></SPAN> </BODY> </HTML> |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Multiplication table function tab() { document.write ("<TABLE>") for (i = 1; i <= 12; i++) { document.write ("<TR>") for (j = 1; j <= i; j++) { ans = i * j document.write ("<TD>" + ans + "</" + "TD>") } document.write ("</" + "TR>") } document.write ("</" + "TABLE>") } </SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="TEXT/JAVASCRIPT"> tab() </SCRIPT> </BODY> </HTML> |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Calculate interest until total doubles function compound() { var rate = document.comp.intr.value var tot = 1.00 var years = 0 while (tot < 2) { tot = tot + (tot*rate/100) years++ } document.comp.doub.value = years } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME=comp> Enter interest % <BR><BR><INPUT TYPE=TEXT NAME=intr SIZE=4> <BR><BR><INPUT TYPE=BUTTON VALUE="How long to double?" ONCLICK="compound()"> </FORM> <BR><BR><INPUT TYPE=TEXT NAME=doub SIZE=3 DISABLED> years </BODY> </HTML> |
<IMG SRC='col0.gif' ALT=' ' NAME='row1col1' ONMOUSEDOWN=changemos(1,1)>
"col0.gif" is a picture file which is plain white. The image is given the NAME of "row1col1", and if you click on it, the code of "changemos(1,1)" is actioned. However, there are 100 of these elements, and every element has a different name, and calls "changemos" with different values. You could code each one separately, but what a lot of code! So I set up loops to do this, using document.write (you can't do loops in normal HTML). I've made the setting up of the mosaic elements in the <HEAD> ... <HEAD> as a function, and called this in the <BODY>...<BODY>, but if you prefer, you could put the code directly within the <BODY>...<BODY> - but within <SCRIPT>...<SCRIPT> of course. I've made the contents of the document.write grey in my colour code rather than the HTML colours, since it really it a literal rather than HTML at this stage. It only gets actioned as HTML when the page gets loaded. Making it grey allows me to shows how you can build up a string of HTML with variables in it, so enabling me to produce all 100 elements, each with its own unique NAME and call to "changemos" - very clever!
![]() |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Global variables col = 1 // Set up mosaic function setmos() { document.write ("<TABLE BORDER>") for (i = 1; i <= 10; i++) { document.write ("<TR>") for (j = 1; j <= 10; j++) { document.write ("<TD>") document.write ("<IMG SRC='col0.gif' ALT=' ' ") document.write ("NAME='row" + i + "col" + j + "'") document.write (" ONMOUSEDOWN=changemos(" + i + "," + j + ")>") document.write ("</" + "TD>") } document.write ("</" + "TR>") } document.write ("</" + "TABLE><BR>") } // Set up colours function setcol() { document.write ("<TABLE BORDER><TR><TD>") for (k = 0; k <= 8; k++) { document.write ("<IMG SRC='col" + k + ".gif' ALT=' ' ") document.write (" ONMOUSEDOWN=changecol(" + k + ")>") } document.write ("</" + "TD>" + "</" + "TR>" + "</" + "TABLE>") } // Change a mosaic tile function changemos(i,j) { document.images['row' + i + 'col' + j].src = 'col' + col + '.gif' } // Change colour function changecol(k) { col = k document.images['currcol'].src = 'col' + k + '.gif' } // Clear whole mosaic function clmos() { changecol(0) for (i = 1; i <= 10; i++) { for (j = 1; j <= 10; j++) { changemos(i,j) } } changecol(1) } </SCRIPT> </HEAD> <BODY> <IMG SRC='col1.gif' ALT=' ' NAME='currcol' ALIGN=LEFT> Current colour - Click in squares<BR><BR> <SCRIPT TYPE="TEXT/JAVASCRIPT"> setmos() setcol() </SCRIPT> <INPUT TYPE=BUTTON VALUE="Clear" ONCLICK="clmos()"> </BODY> </HTML> |
© Jo Edkins 2005