Key Words:function, method, procedure,
form control, input
, id
,
onchange
, event handler, function call, function
definition,
function head, formal parameter, function body, variable, variable
declaration,
assignment operator, assign, getElementById()
, value
,
text, string of characters, +
, string concatenation,
string,
innerHTML
.
See Wikipedia (http://www.wikipedia.org/) for a
detailed
description of the key words.
You are required to have completed the objectives in Lab 6 before doing this lab.
Remember to use the online resources! When you encounter a new HTML element, look at W3Schools XHTML tutorial and W3C's XHTML and HTML recommendations. When you encounter a Javascript function or want to find what you can do with an HTML element in you Javascript code, look at W3Schools Javascript tutorial and W3Schools DHTML/DOM (Dynamic HTML/Document Object Model) tutorial. You do not need to understand everything in their tutorials, but you should at least know where to find help.
The goal of this section of the lab is to practice writing if/else statements.
count
in the
HTML file. Print the variable to the screen and view the results.
(Try to come up with code to do that before looking at the
solution below! It may take you longer, but you will learn
better.)<script type="text/javascript">
var count = 1;
document.writeln("The count is: "+count);
</script>
<script type="text/javascript">
var count = 1;
if (count == 0) {
ready = false;
} else {
ready = true;
count = count-1;
}
alert("Ready: "+ready+", Count is "+count);
</script>
count
to 0.
Reload the page (predict it first!). Enter a number: <input id="count" type="text" onchange="alertMe()"/>You will have to change the variable initialization and assignment for the count variable.
var count = document.getElementById("count").value;Remember that the value will not be available until after the user has typed a value.
Hint: You may need to move your Javascript code into a function!
>if
statement so that
when the count
is greater than 10,
ready
will be false.The goal of this section of the lab is to practice writing iteration statements and become familiar with how the for-statement works. We will also place images, which may be helpful on Project 2.
loopcount
in your
document. Copy in the for-loop as shown below. Try to
understand what it is doing. Notice that in the for-loop, you
initialize
and assign the variable i
, which will be used as an
"iteration variable" (it can have any valid variable name, but
programmers often use i). There are many things to
understand in this short segment of code. Just getting the page
to load is not enough -- it's important to understand how it operates.<script type="text/javascript">
var loopcount = 2;
for (i=0; i< loopcount; i++) {
document.write("Print this text.");
}
</script>
Once you understand what the above code is doing, write your own loop that iterates 5 times, repeating the statement each time:
document.write("The iteration variable "
+ "has the value " + i + "
");
If the loop’s initialization sets i to 0 and limits the iteration to numbers less than 5 and increments by 1 each time, then the result should be
document.write("<img src='lab7-star.jpg' />");
The result should look something like the following