Lecture 6
Lecture 6
Lecture 6
the programmer specifies the sequence in which execution occurs (with some
variability due to input values)
the programmer may have little or no control over when code will (if ever) be
executed, e.g., code that reacts to a button click
there is no set sequence, the page waits for events and reacts
OnLoad & OnUnload
<html>
<!-- form01.html -->
the simplest events are
<head>
<title>Hello/Goodbye page</title> when the page is loaded or
<script type="text/javascript">
unloaded
function Hello()
{
globalName=prompt("Welcome to my page. " +
▪ the ONLOAD attribute of the
"What is your name?","");
BODY tag specifies
} JavaScript code that is
automatically executed when
function Goodbye() the page is loaded
{
alert("So long, " + globalName + ▪ the ONUNLOAD attribute
}
" come back real soon."); similarly specifies JavaScript
</script>
code that is automatically
</head> executed when the browser
leaves the page
<body onLoad="Hello();" onUnload="Goodbye();">
Whatever text appears in the page.
</body>
</html>
view page in browser
HTML forms
most event-handling in JavaScript is associated with form elements
an HTML form is a collection of elements for handling input, output, and
events in a page
<form name="FormName">
…
</form>
<html>
<!-- form02.html -->
<head>
<title> Fun with Buttons</title>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click Me"
onClick="alert('Thanks, I needed that.');" />
</form>
</body>
view page in browser
</html>
Buttons & JavaScript
the ONCLICK event-handler can specify any JavaScript code
▪ can be a sequence of statements inside quotes, can call functions, …
<html>
<!-- form03.html -->
<head>
<title> Fun with Buttons</title>
<script type="text/javascript"
src="http://www.creighton.edu/~davereed/csc551/JavaScript/random.js">
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Lucky Number"
onClick="num = RandomInt(1, 100);
alert('The lucky number for the day is ' + num);" />
</form>
</body>
</html>
OutputWindow.document.close();
}
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Help"
onClick="Help();" />
</form>
</body>
</html>
<html>
<!-- form08.html -->
<head>
<title> Fun with Text Boxes </title>
</head>
<body>
<form name="BoxForm">
Enter a number here:
<input type="text" size=12 name="number" value=2 />
<br /><br />
<input type="button" value="Double"
onClick="document.BoxForm.number.value=
parseFloat(document.BoxForm.number.value) * 2;" />
</form> view page
</body>
in browser
</html>
<html>
<!-- form09.html -->
Text box
<head>
events
<title> Fun with Text Boxes </title>
<script type="text/javascript">
function FahrToCelsius(tempInFahr)
// Assumes: tempInFahr is a number (degrees Fahrenheit)
ONCHANGE
// Returns: corresponding temperature in degrees Celsius triggered when
{ the contents of
return (5/9)*(tempInFahr - 32);
} the box are
</script> changed
</head>
<body>
<form name="BoxForm"> ONFOCUS
Temperature in Fahrenheit: triggered when
<input type="text" name="Fahr" size=10 value="0"
onChange="document.BoxForm.Celsius.value =
the mouse
FahrToCelsius(parseFloat(document.BoxForm.Fahr.value));" /> clicks in the
<tt>----></tt> box
<input type="text" name="Celsius" size=10 value=""
onFocus="blur();" />
in Celsius
</form> blur()
</body> removes focus
</html>
view page in browser
Text box validation
what if the user enters a non-number in the Fahrenheit box?
▪ the verify.js library defines several functions for validating text boxes
▪ WRAP="virtual" specifies that text in the box will wrap lines as needed
Textarea
<!-- form11.html -->
<head>
<title> Fun with Textareas </title>
<script type="text/javascript">
function Table(low, high, power)
example
// Results: displays table of numbers between low & high, raised to power
{
var message = "i: i^" + power + "\n-------\n";
for (var i = low; i <= high; i++) {
message = message + i + ": " + Math.pow(i, power) + "\n";
}
document.AreaForm.Output.value = message;
}
</script>
</head>
<body>
<form name="AreaForm">
<div style="text-align:center">
Show the numbers from
<input type="text" name="lowRange" size=4 value=1 /> to
<input type="text" name="highRange" size=4 value=10 />
raised to the power of <input type="text" name="power" size=3 value=2 />
<br /> <br />
<input type="button" value="Generate Table"
onClick="Table(parseFloat(document.AreaForm.lowRange.value),
parseFloat(document.AreaForm.highRange.value),
parseFloat(document.AreaForm.power.value));" />
<br /> <br />
<textarea name="Output" rows=20 cols=15 wrap="virtual"></textarea>
</div>
</form>
</body> view page in browser
</html>
<html>
<!-- form12.html -->
Textarea example
<head>
<title> Fun with Frames </title>
<script type="text/javascript"
refined
src="http://www.mcs.csuhayward.edu/~bhecker/CS-
3520/Examples/JavaScript/verify.js">
</script>
<script type="text/javascript">
function Table(low, high, power) { /* AS BEFORE */ }
</script>
</head>
<body>
<form name="AreaForm">
<div style="text-align:center">
Show the numbers from
<input type="text" name="lowRange" size=4 value=1
onChange="VerifyInt(this, 1);" /> to
<input type="text" name="highRange" size=4 value=10
onChange="VerifyInt(this, 10);" /> raised to the power of
<input type="text" name="power" size=3 value=2
onChange="VerifyInt(this, 2);" />
<br /> <br />
<input type="button" value="Generate Table"
onClick="Table(parseFloat(document.AreaForm.lowRange.value),
parseFloat(document.AreaForm.highRange.value),
parseFloat(document.AreaForm.power.value));" />
<br /><br />
<textarea name="Output" rows=20 cols=15 wrap="virtual" onFocus="blur();">
</textarea>
</div>
</form>
</body> view page in browser
</html>
More examples
Hoops tournament
▪ text boxes in a table to form brackets
▪ users selects teams by clicking on text boxes, automatically filling in brackets
Substitution cipher
▪ text box to enter substitution key
▪ text areas for message & code, generates code at ONCHANGE event
<html>
<!-- frame13a.html -->
<head>
<title>Table of Squares</title>
</head> src="about:blank" loads
<frameset rows="20%,*"> a blank page into the frame
<frame name="Input" src="form13.html"> (ready to be written to)
<frame name="Output" src="about:blank">
</frameset>
</html>
Frame example
<html>
<!-- form13.html -->
<head>
<title> Fun with Frames</title>
<script type="text/javascript">
function Help()
// Results: displays a help message in a separate frame
{
parent.Output.document.open();
parent.Output.document.write("This might be a context-" +
"sensitive help message, depending on the " +
"application and state of the page.");
parent.Output.document.close();
}
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Help" onClick="Help();" />
</form>
</body>
</html>
<head>
<title> Fun with Timeouts </title>
<script type="text/javascript">
function Move()
// Results: sets the current page contents to be newhome.html
{
self.location.href = "newhome.html";
}
</script>
</head>
<head>
<title> Fun with Timeouts </title>
<script type="text/javascript">
function timeSince()
// Assumes: document.CountForm.countdown exists in the page
// Results: every second, recursively writes current countdown in the box
{
// CODE FOR DETERMINING NUMBER OF DAYS, HOURS, MINUTES, AND SECONDS
// UNTIL GRADUATION
document.CountForm.countdown.value=
days + " days, " + hours + " hours, " +
minutes + " minutes, and " + secs + " seconds";
setTimeout("timeSince();", 1000);
}
</script>
</head>
<body onLoad="timeSince();">
<form name="CountForm">
<div style="text-align:center">
Countdown to Graduation 2003 <br>
<input type="text" name="countdown" size=50 value="" onFocus="blur();" />
</div>
</form>
</body>
</html> view page in browser
Cookies & JavaScript
recall that cookies are data files stored on the client machine
▪ can be accessed and/or modified by the server
▪ can also be accessed and/or modified directly by JavaScript code in a page
potential applications:
▪ e-commerce: remember customer name, past visits/purchases, password, …
▪ tutorials: remember past experience, performance on quizzes, …
▪ games: remember high score, best times, …
function delCookie(Attribute)
// Assumes: Attribute is a string
// Results: removes the cookie value under the name Attribute
{
var now = new Date();
document.cookie = Attribute + "=; expires=" + now.toGMTString();
}
<html>
<!-- form17.html
<head>
-->
Cookie
<title> Fun with Cookies </title>
<script type="text/javascript" example
src="http://www.mcs.csuhayward.edu/~bhecker/CS-
3520/Examples/JavaScript/cookie.js">
</script>
<script type="text/javascript">
function Greeting()
// Results: displays greeting using cookie
{
visitCount = getCookie("visits");
if (visitCount == "") {
alert("Welcome to my page, newbie.");
setCookie("visits", 1);
}
else {
visitCount = parseFloat(visitCount)+1;
alert("Welcome back for visit #" + visitCount);
setCookie("visits", visitCount);
}
}
</script>
</head>
<body onLoad="Greeting();">
Here is the stuff in my page.
<form name="ClearForm" align="center">
<div style="text-align:center">
<input type="button" value="Clear Cookie" onClick="delCookie('visits');" />
</div>
</form>
</body>
</html>