Event Handling & Mathematical Methods: (Javascript)
Event Handling & Mathematical Methods: (Javascript)
Methods
(JavaScript)
1
Today’s Goal:
Event Handlers
• To become able to appreciate the concept of
event handlers:
– What are they?
– What do they do?
– How do we benefit from them?
4
JavaScript Handling of Events
• Events handlers are placed in the BODY part of
a Web page as attributes in HTML tags
6
7
<INPUT
type=“submit”
name=“sendEmail”
value=“Send eMail”
onMouseOver=
“if (document.sendEmail.sender.value == “”)
window.alert(‘Empty From field! Please correct’)”
>
9
In-Line JavaScript Event Handling (1)
• Event handlers are placed in the BODY portion
of a Web page as attributes of HTML tags
10
In-Line JavaScript Event Handling (2)
• Multiple JavaScript statements (separated by
semicolons) can be placed in that string, but all
have to fit in a single line; no newline
characters are allowed in that string
11
Another - more sophisticated - way of
accomplishing the same task
12
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function checkForm() {
if ( document.sendEmail.sender.value == “”) {
window.alert( “Empty From field! Please correct” );
}
}
onMouseOver=“checkForm( )”
13
Usage Guideline
• For very short scripts, “all code in the tag”
works well
14
Another event-handling example; this
time from lecture 11
15
16
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function uolWindow() {
window.open(“http://www.uol.edu.pk/”) ;
}
onClick=“uolWindow()”
17
A Few of My Favorite Event Handlers
onClick
onDblClick
onMouseOver
onMouseDown
onFocus
onBlur
onReset
onSubmit
onLoad
onUnload
18
There are many more: there is an
expanded, but still incomplete list in
your book
19
onFocus & onBlur
• onFocus executes the specified JavaScript
code when a window receives focus or when a
form element receives input focus
20
21
JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:
function checkAge( ) {
if( parseInt( document.form1.age.value ) < 12 ) {
window.alert( "Stop! You are younger than 12" ) ;
}
}
• <BODY onLoad="hello()">
• </BODY>
• </HTML>
27
28
<HTML>
<HEAD>
<TITLE>onLoad Demo</TITLE>
<SCRIPT type=“text/JavaScript">
var seconds = 0;
// called when the page loads to begin the timer
function startTimer() {
// 1000 milliseconds = 1 second
window.setInterval("updateTime()", 1000);
}
document.forms.frmcontact
32
Text Fields
• Main properties
myTextField.value
• Main Events
onfocus (the element trigger onfocus when go into it)
onblur (trigger when you leave it, onblur is always call
when you leave the field)
onchange, onkeypress, onkeydown, onkeyup
33
CheckBoxes and Radio Buttons
• Main properties
myCheckBox.checked (true or false)
• Main Events
onclick
onchange
34
Form Events
Main Events
onsubmit
35
A Note on Syntax (1)
• Mixed-case capitalization of event handlers
(e.g. onClick) is a convention (but not a
requirement) for JavaScript event handlers
defined in HTML code. Using ‘ONCLICK’ or
‘onclick’ as part of a an HTML tag is perfectly
legal as well
36
A Note on Syntax (2)
• At times, you may wish to use event handlers in
JavaScript code enclosed in <SCRIPT>,
</SCRIPT> tags
37
A misleading statement from Lecture 11
• I stated:
JavaScript is case sensitive. Only the first of the
following will result in the desired function – the rest
will generate errors or other undesirable events:
– onMouseClick – OnMouseClick
– onmouseclick – ONMOUSECLICK
• Math.round( )
– A method that rounded a number to its nearest
integer
• Math.sin( )
– A method that gave us the sine of an angle
random( )
42
sin( r ), cos( r ), tan( r )
Standard trigonometric functions
EXAMPLE
document.write( Math.cos( Math.PI / 4 ) )
0.7071067811865476
43
asin( x ), acos( x ), atan( x )
Standard inverse-trigonometric functions
EXAMPLE
document.write( Math.asin( 1 ) )
1.5707963267948965
44
sqrt( x ) pow( x, y )
Returns the Returns x
square root of raised to the
x power y
0.5 0.7071 2, 32
4294967296
45
exp( x ) log( x )
Returns Returns the
Math.E raised the natural
to the power x logarithm of x
1 2.718281 Math.E 1
46
round( x ) floor( x ) ceil( x )
Returns Returns Returns
integer largest integer smallest
nearest to x that is less integer that is
than or equal greater than
to x or equal to x
1.1 1 1.1 1 1.1 2
12.5 13 12.5 12 12.5 13
-13.9 -14 -13.9 -14 -13.9 -13
47
abs( x )
Returns the
absolute value
of x
1.1 1.1
-12.5 12.5
00
48
random( )
Returns a randomly-selected, floating-point
number between 0 and 1
EXAMPLE
document.write( Math.random( ) )
0.9601111965589273
49
During Today’s Lecture …
• We looked at the concept of event-driven
programs and event handlers
– What are they?
– What do they do?
– How do we benefit from them?