Java Reference
In-Depth Information
function handle() {
alert(event.type);
}
</script>
This code assigns the
handle()
function to handle the
<p/>
element's
dblclick
event. When the
function executes, it gets the type of event that caused the
handle()
function's execution. Because
the
event
object is global, there is no need to pass the object to the handling function like the DOM
event model. Also note that like other properties of the
window
object, it's not required that you
precede the
event
object with
window
.
Note
Even though you don't have to pass
event
to an event handler, you still
want to do so in order to support both old‐IE and modern browsers.
The same holds true when you assign event handlers through JavaScript using object properties:
<p id="p">Paragraph</p>
<h1 id="h1">Heading 1</h1>
<span id="span">Special Text</span>
<script>
function handle() {
if (event.type == "mouseover") {
alert("You moved over the Special Text");
}
}
document.getElementById("p").ondblclick = handle;
document.getElementById("h1").onclick = handle;
document.getElementById("span").onmouseover = handle;
</script>
Old‐IE does not support
addEventListener()
and
removeEventListener()
, but it does implement
two similar methods:
attachEvent()
and
detachEvent()
. Rewrite Example 5 using old‐IE's event
API:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 10, Example 10</title>
</head>
<body>
<a id="someLink" href="somepage.html">
Click Me
</a>
<script>
var link = document.getElementById("someLink");
function linkClick(e) {