Professional Documents
Culture Documents
Javascript 4 PDF
Javascript 4 PDF
com
JavaScript အားအသံုးျပဳျခင္း
Event Handlers in Javascript
Event handler ေတြဟာ web development အပိုင္းမွာ အရမ္းကို အသံုးှင္ပါတရ္။ Event
Handler ေတြဟာ javascript code ေတြျဖစ္ၿပီး script tag ထဲမွာ သံုးနဳိင္သလို၊ script tag ထဲမေရးပဲလဲ
သံုးနိုင္ပါတရ္။ အခု ပထမဆံုး Event Handler ဘရ္နွစ္မ်ိဳး ရွိတရ္ဆိုတာေလးကို အရင္ ေလ့လာ
ၾကည္႔ၾကရေအာင္။
OnClick Handler
OnClick ရဲ႕ သေဘာ သဘာှကေတာ့ ကလစ္တခ်က္နွိပ္လိုက္တာနဲ႔ မိမိ ေဖာ္ျပေစခ်င္တဲ့
action, အေၾကာင္းအရာ၊ အခ်က္အလက္၊… စတာေလးေတြကို ေဖာ္ျပေစခ်င္တဲ့ အခါမ်ိဳးမွာ
အသံုးျပဳပါတရ္။ အခု ပထမဆံုး onClick handler ကို html tag အတြင္းမွာ အလြတ္ သံုးပံုေလးကို
ေလ့လာၾကည္႔ၾကရေအာင္ ..
<html>
<head><title>onClick</title></head>
<body>
<a href="http://programmingknowledge.blogspot.com" onClick="alert('မဂၤလာပါဗ်ာ။ မိတ္ေဆြ၊ ညီအစ္ကို၊
ေမာင္နွမအေပါင္း အၿမဲတမ္း ေပ်ာ္ရႊင္ ခ်မ္းေျမ႔နုိင္ပါေစ')">Programming Knowledge</a>
</body>
</html>
Result:
ဒါကေတာ့ code ကို run လိုက္တဲ့အခါ ေပၚလာမဲ့ ပံုစံပါ။ ကုဒ္ကို ၾကည္႔လိုက္ပါ။ က်ေနာ္တို႔ Document
Object ထဲကမွ bgColor=“…” method နဲ႔ fgColor=“…” method ကို အသံုးျပဳထားပါတရ္။ Document
<html>
<head><title>onClick with external script </title></head>
<body>
<script>
function information( )
{
window.alert('Welcome to Programming Knowledge. Thank you for visit to.... ');
window.location="http://programmingknowledge.blogspot.com";
}
</script>
မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္နွမအေပါင္း မဂၤလာပါ။ က်ေနာ္ဆိုဒ္ေလးကို သြားခ်င္ရင္ေတာ့ ေအာက္က button ေလးကို
ကလစ္ ေပးလုိက္ပါ။
<form>
<input type="button" name="test" value="Click Here" onclick="information( )">
</form>
</body>
</html>
Result:
ဒါကေတာ့ က်ေနာ္တို႔ code ကို run လိုက္တာနဲ႔ ျမင္ရမဲ့ ျမင္ကြင္းပါ။ က်ေနာ္တုိ႔ Button action ထဲမွာ
onClick ကို information script နဲ႔ ခ်ိတ္ထားတ့ဲအတြက္ေၾကာင့္ button ကို နွိပ္လုိက္ရင္ေတာ့
information function ထဲက statement ေတြ စတင္အလုပ္လုပ္ပါၿပီး ပထမဆံုး ျမင္ေတြ႔ရတာက alert
message ပါ။ ၾကည္႔လိုက္ပါအံုးဗ်ာ။
ဒီမွာ message ကို ဖတ္ၿပီး ok ကို နွိပ္လုိက္ၿပီဆိုရင္ေတာ့ information function ထဲက ဒုတိယ
statement ျဖစ္တဲ့ window.location အလုပ္လုပ္ကို လုပ္ပါၿပီး။ ဒီေတာ့ က်ေနာ္တို႔ location မွာ
ညႊန္းထားတဲ့ webpage ၾကီး ေပၚလာပါလိမ့္မရ္။ ၾကည္႔လိုက္ပါအံုး။