• No results found

Lec 16 JavaScript - VI.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 16 JavaScript - VI.pptx"

Copied!
50
0
0

Loading.... (view fulltext now)

Full text

(1)

CS1113 Web Programming

Lecture 16

Event Handling & Mathematical

Methods

(2)

Today’s Goal:

Event Handlers

To become able to appreciate the concept of

event handlers:

What are they?

What do they do?

(3)

What is Event Handling?

Capturing events and responding to them

• The system sends events to the program and

the program responds to them as they arrive

• Events can include things a user does - like

(4)

Event Driven Programs

Programs that can capture and respond to

events are called ‘event-driven programs’

JavaScript was specifically designed for writing

such programs

(5)

JavaScript Handling of Events

Events handlers are placed in the BODY part of

a Web page as attributes in HTML tags

Events can be captured and responded to

directly with JavaScript one-liners embedded in

HTML tags in the BODY portion

• Alternatively, events can be captured in the

(6)
(7)
(8)

<INPUT

type=“submit”

name=“sendEmail” value=“Send eMail”

onMouseOver=

“if (document.sendEmail.sender.value == “”)

window.alert(‘Empty From field! Please correct’)”

(9)

That was event handling through

what we may call

‘in-line JavaScript’

(10)

In-Line JavaScript Event Handling (1)

Event handlers are placed in the BODY portion

of a Web page as attributes of HTML tags

The event handler attribute consists of 3 parts:

1. The identifier of the event handler 2. The equal sign

(11)

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

• Due to this limitation, sophisticated event

handling is not possible with in-line event

(12)
(13)

onMouseOver=“checkForm( )”

JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:

JavaScript included as an attribute of the “Send eMail” button:

function checkForm() {

if ( document.sendEmail.sender.value == “”) {

window.alert( “Empty From field! Please correct” );

(14)

Usage Guideline

For very short scripts, “all code in the tag”

works well

The “code in the HEAD portion” is the right

choice for developing larger JavaScript scripts

It makes the code easier to read

(15)
(16)
(17)

onClick=“uolWindow()”

JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:

JavaScript included as an attribute of the “New Window” button:

function uolWindow() {

(18)

A Few of My Favorite Event Handlers

onClick

onDblClick

onMouseOver onMouseDown

onFocus onBlur

(19)

There are many more: there is an

expanded, but still incomplete

list in

your book

(20)

onFocus & onBlur

onFocus executes the specified JavaScript

code when a window receives focus or when a

form element receives input focus

• onBlur executes the specified JavaScript code

(21)
(22)

JavaScript that goes between the <SCRIPT>, </SCRIPT> tags:

JavaScript included as an attribute of the INPUT tag:

function checkAge( ) {

if( parseInt( document.form1.age.value ) < 12 ) {

window.alert( "Stop! You are younger than 12" ) ; }

(23)

<HTML><HEAD>

<TITLE>onBlur( ) Demo</TITLE>

<SCRIPT>

function checkAge() {

if( parseInt(document.form1.age.value) < 12) {

window.alert("Stop! You are younger than 12" ) ; }

}

</SCRIPT>

</HEAD>

<BODY bgcolor="#66FFCC">

<FORM name="form1" method="post" action=""> <TABLE border="1">

<TR> <TD>Age</TD>

<TD><INPUT type="text" name="age" onBlur="checkAge()"> </TD></TR><TR> <TD>Phone Number</TD>

<TD><INPUT type="text" name="phNo"></TD>

(24)

onLoad & onUnload

onLoad executes the specified JavaScript code

when a new document is loaded into a window

onUnload executes the specified JavaScript

code when a user exits a document

(25)

• The onUnload attribute fires once a page has

unloaded (or the browser window has been closed).

onUnload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser

window, etc.)

(26)
(27)

<!DOCTYPE html><HTML>

<TITLE>Example of onLoad Event Handler</TITLE><HEAD>

<SCRIPT type=“text/javascript">

function hello(){

window.alert("Hello there...\nThis is an example of onLoad.");

}

</SCRIPT>

</HEAD>

<BODY onLoad="hello()">

(28)
(29)

<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); }

// called every 1000 ms to update the timer function updateTime() {

++seconds;

document.getElementById("time").innerHTML = seconds; }

</SCRIPT> </HEAD>

(30)

More Uses for onLoad/onUnload?

• onLoad can be used to open multiple Windows

when a particular document is opened

• onUnload can be used to say “Thank you for

the visit” when a user is leaving a Web page

• At times, a user opens multiple inter-dependent

(31)

Enhancing Forms with JavaScript

We do care about several things.

First values of individual form elements.

Event cause by those form element.

Event of entire form itself particular submitting the form.

You have given form and field inside it

their own ids then you can ofcourse use

(32)

Or if your HTML form have either the name

property, you can use following format too:

(33)

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)

(34)

CheckBoxes and Radio Buttons

Main properties

myCheckBox.checked (true or false)

Main Events

(35)

Form Events

(36)

A Note on Syntax (1)

Mixed-case capitalization of event handlers

(e.g. onClick) is a convention (but not a

(37)

A Note on Syntax (2)

At times, you may wish to use event handlers in

JavaScript code enclosed in <SCRIPT>,

</SCRIPT> tags

• In those cases you have to strictly follow the

JavaScript rule for all event handler identifiers: they must all be typed in small case, e.g.

(38)

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

(39)

MATH Object

Math.PI

A property that gave us the value of Pi

• Math.round( )

A method that rounded a number to its nearest integer

Math.sin( )

(40)

Mathematical Functions in JavaScript

• In addition to the simple arithmetic operations

(e.g. +, *, etc.) JavaScript supports several

advanced mathematical operations as well

• Notationaly, these functions are accessed by

referring to various methods of the Math object

(41)
(42)

Methods

sin( r ) cos( r ) tan( r ) asin( x ) acos( x ) atan( x )

atan2( x, y ) max( x, y ) min( x, y ) round( x ) floor( x ) ceil( x ) exp( x )

log( x ) abs( x ) sqrt( x )

(43)

sin(

r

)

,

cos(

r

)

,

tan(

r

)

Standard trigonometric functions

Returns the sine, cosine or tangent of ‘r’, where ‘r’ is specified in radians

EXAMPLE

(44)

asin(

x

)

,

acos(

x

)

,

atan(

x

)

Standard inverse-trigonometric functions

Returns the arcsine, arccosine or arctangent of ‘r’ in radians

EXAMPLE

(45)

Returns the square root of x

sqrt(

x

)

0.5  0.7071

Returns x

raised to the power y

pow(

x, y

)

2, 32 

(46)

Returns

Math.E raised to the power x

exp(

x

)

1  2.718281

Returns the the natural logarithm of x

log(

x

)

(47)

ceil(

x

)

Returns integer

nearest to x

Returns

largest integer that is less

than or equal to x

Returns smallest

integer that is greater than or equal to x

floor(

x

)

round(

x

)

1.1  1

12.5  13

1.1  1

12.5  12

1.1  2

(48)

Returns the

absolute value of x

abs(

x

)

(49)

random( )

Returns a randomly-selected, floating-point number between 0 and 1

EXAMPLE

document.write( Math.random( ) )

(50)

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?

• We wrote simple programs to demonstrate the

References

Related documents

scholars who write about fat (Cressida Heyes, Susan Bordo, Robyn Longhurst) write about the painfulness of wanting social justice for fat people while also losing weight themselves.

Korea Laboratory Accreditation Scheme (KOLAS) SCOPE: Testing ISO/IEC 17025. Calibration

In this unique community-based sample of AA and white men and women assessed for hip morphology using a standardized method, we found that case hips tended to have higher AP

finger scanner ekey net surface-mounted finger scanner ekey net TERMINAL SERVER ekey converter LAN RS485 User B SUBSIDIARY ekey net control panel mini.. ekey

The aim of our study was to assess the performance of abdominal ultrasound carried out at the patient’s bedside by an emergency physician compared with a

On imaging studies, fat should be clearly visible in the orbital apex (best seen in coronal and axial plane), along the roof of the orbit (best seen in sagittal and coronal plane),

On the other hand, VG pairs were placed beside each other on the x-y plane far enough where there is no interaction between the vortices where the heat transfer increased

sharp lateral edges “shield” the wake separation region at the base and enable a “two-dimensional” separation, “2D” meaning that the mean streamlines at