Java Scriptobject Event 2
Java Scriptobject Event 2
Javascni
document
98 Wed
the user
and the
between
relationship that
event is a
to
handle
an m e c h a n i s m
In general events
and a
s e t of
tines the
propenie
5.5.1 Event Object hence with
some
and
e v e n t as
an object the event.
every about
considers (details)
information
Javascript the
contains
the event object properties.
events and the
illustrates s o m e
Table 5.5.I
and Their Properties
Some Events
Table 5.5.1
Property
Enen X Y co-ordinate
of the point ofclick with reference
left buton of the
mouse.
The user cicks the to the screen
ASCII value of the key pressed.
The wser presses a keg
Propernie
Evem IE Targe
NS
Abort NS image The user aborts the loading of the
IE image by clicking another link or
the stop button
Blur IE windo User removes input focus fromn
NS frame Window frame or form Type
all form elements
Click NS button The user cicks with the left Target
E radio button button of the mouse Type
check box Clhent
li
Change t n field
User changes value of ficid Targe
Typ
select list Chenc
Cient
DbiClick SrcE
documen
The user double clicks with Targ
eft butnon of mouse Typ
Objects and Even1s 99
link
ClientX
ClientY
SrcElement
NS window The user drags and drops a file on
DragDrop
to the browser
NS image Attempt to load a document or Returm value
Error
IE Window image causes an erTor SrcElement
link Type
object
script
style ClientX
The user brings input focus to the
NS window
Focus ClientY
IE frame target Return value
all form elements ScreenX
ScreenY
SrcElement
Type
The user hits F1
NS window
Help Key Return value
IE The user initiates a key press
document X.Y
IE
KeyDown ClientX
NS image ClientY
link ScreenX
textarea ScreenY
Returm value
continues pressing a key
The user X.Y
document ClientX
KeyPress IE
image ClientY
NS
ScreenX
link
ScreenY
textarea
Return value
key press
user
releases X.Y
The
ClientX
document
KeyUp IE ClientY
NS image ScreenX
link ScreenY
document ClientY
Load IE ScreenX
applet
NS Contd.)
embed
frame set
img
100 Web Technology and
Desig
Table 5.5.2 (Contd.)
5
Event IE Target Cause Properties
NS A
ScreenY lo
link
me
SCrnpt
style
window
User initially depresses mouse Return value
MouseDow IE button
button(left or right) X,Y
NS document
link Clientx
ClientY
ScreenX
ScreenY
MouseMove IE window The mouse is moved Return value
NS document X,Y
ClientX
ClientY
ScreenX
SereenY
MouseOut IE area Mouse is moved out of focus Return value
NS layer X.Y
link ClientX
ClientY
ScreenX
ScreenY
MouseOver IE area User moves mouse pointer over Return value
NS layer a target element X,Y
link ClientX
ClientY
ScreenX
ScreenY
MouseUp IE User releases the
NS
mouse
key(either)
after press
Reset NS form User resets a form
IE
Resize NS window The frame or window is
IE frame
Select resized
NS text
User selects/
IE textarea
highlights the text
htmlarea
Submit NS form
IE
User submits a form
Unload NS document The
IE user exits to a different page
frameset
image
window
Objects and Events 101
Double Click (dblclick Event
5.5.2
MyComputer
Done Click Event
Double
5.18
Figure
<html>
</h4>
X a v i e r < b r >
C.
h4 >Web Technology book by Dr.
O l o g y
Design k e
Technolog)y
and press
Press Fl
F1 key for
.
Web
102 onHelp.
te
page
to 1 I Iu s t r a
is a sampl e
This
<h5
<br<br> </h5
</h5»
i -MB n
<body> code
the city " C i t y c o d e "
\n Mumba )">
<h5 Type type-"text
" name=
-CH \n
D e l h i - D L
Chennai
input "Citycode
"alert (
onHelp-
body>
html>
5.19.
shown in Figure
The output is
[WorkingOffinel
help.html-
Microsoft InternetExplorer
Cwebtechnology
Mew Favorites Tools Help
Fle Edt
Search Favorites
History w
Back
0 DAP
Address C:webtechnology\help.html
5.5
Done AK
My Computer this
Figure 5.19 On
HelpEvent
5.5.4 Key Press Event
A keypress event
lustrate this event. occurs when the user continues pressing a key. ple t
Let us see
<html>
a
<h4>web
Technology book by Dr.
C.
Xavier<br> T
Objects and Events 103
<h5: This is a
sample page to
illustrate
Press a key 1n the document </h5> Keypress.<br><br>
ndy onkeypress= " alert("Hi. .you have
</body> pressed a
Key') "
/html»
OK
e Done My
Computer
Figure 5.20 Key Press Event
[wko Ofmine
Explorer
webtechnology keyup ht MiKrovoft Indevet
Ple Ede Vie Favorites ools Help
OKOK
e] Done
My Computer
Figure 5.21 Key Up Event
5.5.6 Key Down Event
A KeyDown event occurs when the user initiates a key press. An example is shown e
<html>
<h4 Web Technology book by Dr. C. Xavier<br>
<h5> This is a
sample page to illustrate Key Down. <br><br>
Press any key in the
document </h5>
<body onKeyDown=
"alert('Hi. . .You have done Key Down Event >
</body
</html>
3D y Computer
Figure 5.22 Key Down Event
5.5.7 Change Event
occurs. So, we can call the
he user selects item from a Select Box, a change event
an
<html>
<head»
<title>Telephone Directory</title
</head»
<body»
<sCript»
var i, f,phone;
function getnumber (f)
i=f.nameBox.selectedIndex;
f.PhoneNumber. value=phone [ il
</SCript»
<form name=telephone form>
pre>
<center>
<hr>
<h2> TELEPHONE DIRECTORY </h2>
pre>
<center><hr>
<br>
select the name to get the Telephone number
<br>
NAME: <select name=nameBox
<option>Aparna</opti on» onChange=getnumber ( this.form)>
<option>Nithya</option»
<option>Priya</option»
<option>Sandhya</option>
<option>Shruthi</option>
<option>Sumathi</option>
<option»Gowri</option»
option>Kr ithika</options
option>Rekha</option>
<Option>Reena</option>
seleCt>
Objects and Events 107
DHONE NUMBER <input type=
textt name-=
<br><br><hr><br><br> PhoneNumber>cbr>
</torm>
</body>
</html>
Back Foward Stop Refresh Home Search Favorites History Mail Primt
Addressl Prof2\cwebtechnology\telephone chan.htm
MCGo Links
TELEPHONE DIRECTORY
NAME :Aparna
PHONE NUMBER :6184723
Local intranet
below:
The HTML document is given
<html>
<head ">
text/javascript
S C r i p t type= "
function setnamefocus ()
.namefield. focus ()
document.forms [0]
function setaccountfocus ()
. a c c o u n t f i e l d . f o c u s ()
document.forms [0]
function setcityfocus ()
</script»
</head>
<body»
<h2>This form illustrates the focus on an object</h2>
<form>
Name .
<input type= "
text" name= "namefield" size="30">
<br»
Account Number. .<input type= " text" name=
<br>
"accountfield" size="10">
City 1nput type= "text" name=
<br» "cityfield" size="10
<input type="button" value= "Name Search"
<input type="button" value= onclick=" setnamefocus
" Account Search"
<input type="button" onclick= setaccourL "
</ form>>
value= "City Search" onclick=" () *>
setcityfOCus
</body>
</html>
avier
File Edit
vascript'FocusFocus.html-Microsoft
View Favorites Tools
Help
Internet Explorer
O
Search FavortesHistory
Address] D:\Xavier\JavaScript\Focus\Focus.htmi
soLinks
This form illustrates the focus on an object
Name
Account Number.|
City
Neme Search Account Search City Search
Done My Computer
Figure 5.24 Focus Method
eselect() method is used to select the text in a text field. This example illustrates the select
box and a submit button. The text box is
Cus on an object.In this document we have a text button the setfocus () function
edto St.Xavier's College .On clicking the 'Enter College' focus to it.
also sets
.Ihis function selects the value of text box and
The HTML document is given below:
head
<Cript type-" ext/javascript ">
r.ction setfocus )
lscript>
</'head
of the college</h2
h2>Please
name
the
then type
button and
**CR Ehe
I10 Wed TeckmologY ani Iksig
o-s0 st.Xavier's Colleg
Xaviere
V a l u e -" S t .
nput type: Nt
nama"°o)lege
oncli ck-"set tocus () "
Oiege
*button"
value *Ent o
YDe-
orm>
body
htmi
Figure 5.25.
The output is shown in
\JavaScript\Focus\Select.html-Microsoft Internet
Explorer O
D:Xavier
View Favorites Tools Help
a Search Favorites History L 4
dress E) D:}Wavier\Javaacript\Focus\Select.html GoUnks
Please click the button and then type the
name of the college
StXavier's Collegee Enter College
e Done
My Computer
Figure 5.25 Select Method
5.6 Browser Object
document.write("BROWSER: " )
document.wrlte (navigator.appName +"<br»")
document.write ( "BROWSERVERSION: ")
Ohjects and Events 111
document.
t.write
navigator.appVersion +
"<br»")
document.write( "CODE: "
cument. write (navigator.appCodeName
document.write("PLATFORM:
"<br>")
JavaScript\0bjects\BrowserObject.html- Microsoft
Internet Explorer
D:Xavier\
File Edit View Favorites Tools Help
details
Dear user, Here are your browser
<html>»
<head
<Script type="text/javascript">
function validate ()
indexin=document.resume.email.value.indexof ( "e")
agein=document. resume.age.value
namein=document.resume.namel0.value
if (indexin==-1)
if (agein«1 1I agein>150)
if (namein. length>10)
return true
</script>
</head
cbody>
<h4»
This form is designed to illustrate data in
Javascript by C.Xavier
validation practices
</h4>
Objects and Events 113
farm name= "esume onsubmit-" ret:
validate()*s
<h5>
Enter your e-mail:
3Done on
My Computer
Submit
Data Validation
Figure 5.27
114 web Technology and Design
Exercises
in exercise 3.1, include the Javascript to validat idate the data
5.1 In the form mentioned
follows:
Name should have 25 characters and it doesn t have any numbers or specialchet
ters
in
an