8-1 事件??件??器

b@ӺAƥ (Events) q`OsҰϥΪ̪Swʧ@AsiHھکҰ쪺ƥAӶiʧ@C|ҨӻAϥΪIβʷƹAάOsJAiHݦOƥ󪺲͡CSwƥAڭ̥iHbsoAåHSw{ӹ惡ƥ󰵥XA{Y٬uƥBzv(Event handlers)AS٬ CallbackC

@bs`ƥAiC²pUG

bUCdҤAڭ̨ϥ onFocus M onBlur ƥӧܤr쪺eG

Example]onFocus01.htm^G

bWzdҤAunϥΪIrAonFocus ƥQҰʡA쪺eN|ܦuܰڤSOJIFIvCYɦb~NBI@UAonBlur ƥN|QҰʡA쪺eN|ܦuASnڦӥhFIvCWzdҪlɮצpUG

lɡ]onFocus01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>EJMJ</h2>
<hr>

<center>
<input value="IڡI" size=30
	onBlur="this.value='ASnڦӥhFI'"
	onFocus="this.value='ܰڤSOJIFI'">
</center>

<hr>
</body>
</html>

Hint
bWzdҤAthis Nr쥻C

ڭ̥iHϥ body Ҫ onLoad M onUnload ݩʡAөwqbJM}ɥ檺ƥBz{AШUCdҡG

Example]onLoad01.htm^G

Hint
@ss|_uX]block popup windows^ApGAwˤF Google taskbarA]|P˪C]bճoǽdҮɡAATwAsèS_uXAHϽdҹB@`C

AJɡA|X{UCĵiG

۷Anɡ]iOJt@ӺάOs^A|Xt@ĵiG

WzdҪlɮצpUG

lɡ]onLoad01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body onLoad="alert(string1)" onUnload="alert(string2)">
<h2 align=center>ueӡv</h2>
<hr>

<script language="javascript">
string1 = "\n"
         +"@@@@w@@@{@@@@\n"
         +"\n"
         +"@@@@@@@@@@@@@@@\n"
         +"@o̬OJavaScriptuWUA@\n"
         +"@bo̥iHǨnhFF@I\n"
         +"@ƱAor֡I@@@@@  \n"
         +"@@@@@@@@@@@@@@  \n"
         +"\n";
string2 = "oִNnաHŦAӪI";
</script>

<p align=center>
|ueӡvIII

<hr>
</body>
</html>

Hint
A]iHϥ window.onLoad=functionName1 M window.onUnload=function2 ӹFP˪ĪGC

O onUnload äQíwAYnOҦbQeAY@q{XANnϥ onBeforeUnloadAҦpAAiHbb onBeforeUnload wq JavaScript {XH}ҥt@ӵA]CAɡAS|}t@ӵAΦuvAoOUjⱡƪޥ]γ\ANDF...^CШUCdҡG

Example]foreverCockroach01.htm^G

WzdҪlɮצpUG

lɡ]foreverCockroach01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body onBeforeUnload="openNewWin()">

<script>
function openNewWin(){
	// pGW٩TwANLk@A}sC
	// ]ϥζüƨӷW...ҥH@ӴN|Aͤ@ӷsX
	winName = Math.floor(Math.random()*10000);
	window.open('foreverCockroach01.htm', winName, 'location=1,toolbar=1,menubar=1,status=1');
}
</script>

<h2 align=center></h2>
<hr>
A~XD

</body>
</html>

]iHb onBeforeUnload ƥ@}ҤQӵAAӥBٷ|VͶVhI

Hint
۱qڦbWFWzdҤATɴN|HgHӽ|ڡAڷFoӽdҡAL̤o}CAJMOЧAMnuujuvAuNvCD@wn}ܡHMOơAXؤkG
  1. AuvAAsC
  2. IE s JavaScript 䴩GNuu/ںﶵ/w/ۭqh/OXBz/Active scriptingvȮɧ令uΡvAuvA_]wC
  3. qu@޲zNsC
  4. qnﶵΡuTuXvC
oǤkAoAQFܡH

HUoӺXdҡAܤFƺبƥ󪺨ϥΡG

Example]onEvent01.htm^G

@Ӫ󳣥iHܦhPƥAӥBbssAiƥOOVӶVhAаѨWzdҡuH "on" }lʽvC]ѩgTAACXdҤ{XC^


JavaScript {]pPΡGΩΤ