Tuesday, January 18, 2011

Tuesday, January 18th

Here is the javaScript from a program I am writing from a Lynda.Com series of tutorials.


window.onload = newCard;

function newCard() {
if (document.getElementById){
for (var i = 0; i < 24; i++) {
setSquare(i);
}
}
else
alert("You might want to upgrade your browser.")
}

function setSquare(thisSquare) {
var currentSquare = "square" + thisSquare;
var colPlace = new Array(0,1,2,3,4, 0,1,2,3,4, 0,1,3,4, 0,1,2,3,4, 0,1,2,3,4);
var newNum = (colPlace[thisSquare] * 15) + getNewNum() + 1;
document.getElementById(currentSquare).innerHTML = newNum;
}

function getNewNum(){
return Math.floor(Math.random() * 15)
}


Here is the HTML for onmouseover and onmouseout


<body>
<a href="Lab02.html" onmouseover="document.arrow.src = 'images/arrow_on.png'"
onmouseout = "document.arrow.src='images/arrow_off.png'"><img src="images/arrow_off.png" name="arrow" alt="arrow" /></a>
</body>

2 comments:

Brad Jensen said...

i expect the window.onload gets frired when the window loads, but I don't know where in the html to put it to make this happen.

I assume the rest of the script can be put in the <-head-> part of the page. ( changed the head tag since this blog won't let me pu the actual tag in the comments).

it looks like it is generating 24 squares at random, but I don't undeestand how the getelementbyid can find the squares if they don't alread exist in the html page somewhere.

I would make a page to try out the javascript, but don't know how.

aljensen said...

There's a lot of html but I didn't post it since it wasn't relevant - a table with cells for each number. This is on a separate page that imports that javascript in.