Lecture 8 Javascript
Lecture 8 Javascript
user-defined classes
encapsulation, but no data hiding
event-driven programming
onLoad/onUnload
onClick, onMouseOver, onMouseOut
Classes vs. objects
in programming terminology:
for both String and Array, can assign constant values directly, hide call to new
numbers = [1, 2, 3, 4]; // creates and initializes
firstName = "Dave"; // same as above, but cleaner
Date class
the Date class can be used to access the date and time
constructors include:
methods include:
<html>
<head>
<title>Time page</title>
</head> Here, set to the current date and
time using the default
<body>
Time when page was loaded: constructor
<script language="JavaScript">
now = new Date();
toString displays the full date
document.write(now.toString() + "<br><br>"); using month and day names
</html>
Date example (cont.)
<html>
<head>
<title>Time page</title>
</head>
document.write(hours + ":" +
• need to handle 12 AM special
now.getMinutes() + ":" +
now.getSeconds() + " " +
time);
</script>
</body>
</html>
Another example
<html>
<head>
<title>Time page</title>
</head>
</html>
document object
Both IE and Netscape allow you to access information about an
HTML document using the document object (Note: not a class!)
<html>
document.write(…)
<head>
<title>Documentation page</title> method that displays text
</head> in the page
<body>
<table width="100%">
<tr> document.URL
<td><small><i> property that gives the
<script language="JavaScript"> location of the HTML
document.write(document.URL); document
</script>
</i></small></td>
<td align="right"><small><I>
<script language="JavaScript"> document.lastModified
document.write(document.lastModified); property that gives the date
</script> & time the HTML
</i></small></td> document was saved
</tr>
</table>
</body>
</html>
navigator object
can access information about the browser being used to access the
Web page using the navigator object (Again: not a class!)
"Netscape"
"Microsoft Internet Explorer"
a {font-family:Arial;
color:white;
background-color:red}
User-defined classes
can define new classes, but the notation is awkward
simply define a function that serves as a constructor
specify data fields & methods using this
<script language="JavaScript"
src="Die.js"> create a Die object using
</script> new (similar to String and
</head> Array)
<body>
<script language="JavaScript"> here, the argument to Die
die6 = new Die(6); initializes numSides for
die8 = new Die(8); that particular object
roll6 = -1; // dummy value to start loop
roll8 = -2; // dummy value to start loop each Die object has its own
while (roll6 != roll8) { properties (numSides &
roll6 = die6.Roll();
roll8 = die8.Roll();
numRolls)
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>
<head>
<title>Hello/Goodbye page</title>
the simplest events are
when the page is loaded
<script language="JavaScript">
function Hello() or unloaded
{
userName=prompt("Welcome to my page. " + the ONLOAD attribute of
"What is your name?",""); the BODY tag specifies
} JavaScript code that is
automatically executed
function Goodbye() when the page is loaded
{
userName=alert("So long, " + userName + the ONUNLOAD attribute
" come back real soon."); similarly specifies
} JavaScript code that is
</script> automatically executed
when the browser leaves
</head> the page
<body onLoad="Hello();" onUnload="Goodbye();">
Whatever text appears in the page.
</body>
</html>
<html>