Event handler What are event handlers? They can be considered as triggers that
execute JavaScript when something happens, such as click or move
your mouse over a link, submit a form etc.
onClick onClick handlers execute something only when users
click on buttons, links, etc. Let's see an example:
<script>
function ss()
{
alert("Thank you!");
}
</script>
<form>
<input type="button"
value="Click here" onclick="ss()">
</form>
The function ss() is invoked when the user clicks the button. Note:
Event handlers are not added inside the <script> tags, but
rather, inside the html tags.
onLoad The onload event handler is used to call the execution
of JavaScript after loading:
<body onload="ss()">
<frameset onload="ss()">
<img src="whatever.gif" onload="ss()">
onMouseover,onMouseout These handlers are used exclusively
with links.
<a href="#"
onMouseOver="document.write('Hi, nice to see you!">
Over Here!</a>
<a href="#" onMouseOut="alert('Good try!')">Get Out Here!</a>
onUnload onunload executes JavaScript while someone leaves
the page. For example to thank users.
<body onunload="alert('Thank you for visiting us. See you soon')">
Handle multiple actionsHow to have an event handler be able
to call multiple functions/statements? That's simple. You just need
to embed the functions inside the event handler as usual, but separate
each of them using a semicolon:
<form>
<input type="button" value="Click here!" onClick="alert('Thanks
for visiting my site!');window.location='http://www.yahoo.com'">
</form>
|