l08 Js Dom Forms
l08 Js Dom Forms
Contents
• Window Object
• DOM and Events
• Class and Objects
• HTML Form
• Events
Window Object
• Represents an open window in a browser
• Many window object properties and methods area available
• No public standard but major browsers support it
• If a document contain frames, then there is
one window object, window, for the HTML document • and one additional window object for
each frame,
accessible via an array window.frames
• Methods provided by a window object include
• close() - closes a browser window/tab
• focus() - give focus to a window (bring the window to the front)
• blur() - removes focus from a window (moves the window behind others)
• print() - prints (sends to a printer) the contents of the current window
• Window Object: Dialog Boxes
Example:
alert("Local time: " + (new Date).toString())
• And many more…
navigator Object
<html>
navigator.appName <!–- CSE391 js14.html -->
Before After
User-Defined Classes
• JavaScript is an object-oriented language, but one without classes
• Instead of defining a class, we can define a function that acts as object
constructor
specify data fields & methods using this
no data hiding: can't protect data or methods
define Die function (i.e.,
// CSE391 Die.js // constructor)
// Die class definition
//////////////////////////////////////////// initialize data fields in the
function Die(sides) function, preceded with this
{
this.numSides = sides; similarly, assign method to
this.numRolls = 0;
separately defined function
this.Roll = Roll; // define a pointer to a function
} (which uses this to access
data)
function Roll()
{
this.numRolls++;
return Math.floor(Math.random()*this.numSides) + 1;
}
<html>
<!–- js15.html -->
<head>
Class Example
<title>Dice page</title>
the programmer specifies the sequence in which execution occurs (with some variability due
to input values)
• computation within a Web page is rarely serial instead, the page reacts to
events such as mouse clicks, buttons, …
much of JavaScript's utility is in specifying actions that are to occur in the page as a result
of some event
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 12.10.2006 --> the simplest events are when
<head>
the page is loaded or unloaded
<title>Hello/Goodbye page</title>
the onload attribute of the
<script type="text/javascript"> <body> tag specifies JavaScript
function Hello()
{
code that is automatically
globalName=prompt("Welcome to my page. " + executed when the page is
"What is your name?",""); loaded
}
the onunload attribute similarly
function Goodbye() specifies JavaScript code that is
{ automatically executed when the
alert("So long, " + globalName +
" come back real soon.");
browser leaves the page
}
</script>
</head>
<html>
<!–- form02.html 12.10.2006 -->
<head>
<title> Fun with Buttons</title>
<script type="text/javascript"
src="JS/random.js">
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Lucky Number"
onclick=“var num = RandomInt(1, 100);
alert('The lucky number for the day is ' + num);" />
</form>
</body>
</html>
Buttons & Functions
<html>
<!–- form03.html 13.10.2006 --> for complex tasks,
<head>
<title>Fun with Buttons</title>
should define function(s)
and have the onclick
<script type="text/javascript">
function Greeting() event trigger a function
// Results: displays a time-sensitive greeting call
{
var now = new Date();
if (now.getHours() < 12) {
alert("Good morning");
}
else if (now.getHours() < 18) {
alert("Good afternoon");
}
else {
alert("Good evening");
}
}
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Greeting"
onclick="Greeting();" />
</form>
</body>
</html>
Buttons & Windows
• alert boxes are fine for displaying short, infrequent messages
not well-suited for displaying longer, formatted text
not integrated into the page, requires the user to explicitly close the box
<head>
<title> Fun with Buttons </title>
<script type="text/javascript">
function Help()
// Results: displays a help message in a separate window
{
var OutputWindow = window.open();
OutputWindow.document.open();
OutputWindow.document.close();
}
</script>
</head>
<body>
<form name="ButtonForm">
<input type="button" value="Click for Help"
onclick="Help();" />
</form>
</body>
</html>
Text Boxes
• a text box allows for user input
unlike prompt, user input persists on the page & can be edited
<html>
<!-– form06.html 13.10.2006 -->
<head> <title> Fun with Text Boxes </title> </head>
<body>
<form name="BoxForm">
Enter your name here:
<input type="text" name="userName" id=“userName” size=“12” value="" />
<br /><br />
<input type="button" value="Click Me"
onclick="alert('Thanks, ' + document.BoxForm.userName.value +
', I needed that.');" />
</form>
</body>
</html>
Read/Write Text Boxes
• similarly, can change the contents with an assignment
Note: the contents are raw text, no HTML formatting
Also: contents are accessed as a string, must parseFloat or parseInt if want a
number
<html>
<!-–form07.html 13.10.2006 -->
<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>
</body>
</html>
Text Box Events
<html>
<!-– CSE391 form08.html 13.10.2006 -->
<head>
<title> Fun with Text Boxes </title>
<script type="text/javascript">
onchange
function FahrToCelsius(tempInFahr) triggered when
// Assumes: tempInFahr is a number (degrees Fahrenheit) the contents of
// Returns: corresponding temperature in degrees Celsius
{ the box are
return (5/9)*(tempInFahr - 32); changed
}
</script>
</head>
onfocus
<body>
<form name="BoxForm">
triggered when
Temperature in Fahrenheit: the mouse clicks
<input type="text" name="Fahr" size=“10” value=“0" in the box
onchange="document.BoxForm.Celsius.value =
FahrToCelsius(parseFloat(document.BoxForm.Fahr.value));" />
<tt>----></tt>
<input type="text" name="Celsius" size=“10” value="" blur()
onfocus="blur();" /> removes focus
in Celsius
</form>
</body>
</html>
Text Areas
• a TEXT box is limited to one line of input/output
• a TEXTAREA is similar to a text box in functionality, but can specify any number
of rows and columns
<head>
<title> Fun with Text Boxes </title>
<script type="text/javascript"
src="JS/verify.js">
</script>
<script type="text/javascript">
function FahrToCelsius(tempInFahr)
{
return (5/9)*(tempInFahr - 32);
}
</script>
</head>
<body>
<form name="BoxForm">
Temperature in Fahrenheit:
<input type="text" id="Fahr" size=“10” value=“0”
onchange="if (VerifyNum(this)) { // this refers to current element
var F=document.getElementById(‘Fahr’);
document.BoxForm.Celsius.value =
FahrToCelsius(parseFloat(F.value));
}" />
<tt>----></tt>
<input type="text" name="Celsius" size=“10” value=“”
onfocus=“getElementById(‘F’).focus();" />
in Celsius
</form> </body>
</html>
Check Boxes and Radio buttons
• Radio buttons are similar to check boxes, but only one of them can be selected at any time.
• They are defined by <input type=“radio”> tags (similar to the checkbox tags in the previous example,
with similar properties) and accessed in the same manner.
JavaScript & Timeouts
• the setTimeout function can be used to execute code at a later time
setTimeout(JavaScriptCodeToBeExecuted, MillisecondsUntilExecution)
<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>
</body>
</html>
Exercise
• Exercise
• Design a form which calculate sum of two integers given by the user
• How to access Cookie with JS?
• READINGS/Practice
M Schafer: Ch. 19, 20, 22
https://www.w3schools.com/js/default.asp
http://www.csc.liv.ac.uk/~martin/teaching/comp519/NOTES/JavaScript.pdf
Acknowledgement
• This module is designed and created with the help from following
sources-
https://cgi.csc.liv.ac.uk/~ullrich/COMP519/
http://www.csc.liv.ac.uk/~martin/teaching/comp519/
Anup Majumder, Jahangirnagar University