HTML and CSS Reference
In-Depth Information
Figure 13.32 The user clicked the Internet Explorer button.
13.7.4 Passing Events to a JavaScript Function
As stated earlier, JavaScript registers events as they occur within the document and reg-
isters information about that event such as what key was pressed, what mouse button
clicked, and so on. You can pass this information directly to a JavaScript function. The
problem is that Internet Explorer has its own way of tracking events and the other major
browsers use the W3C standard (e.g., Firefox and Opera). With the W3C method, the
event object is sent as a parameter to the JavaScript function, but with Internet Explorer
the event is a property of the window object ( window.event ). It is easy enough to check
for both without interrupting the flow of your program by testing for both conditions.
The following line uses the ternary operator to test for both events and for null. (The
ternary operator was described in detail in section “The Conditional Operator” on
page 108, Chapter 5.)
function testEvent(e){
var evt = (e) ? e: ((window.event)? window.event: null)
}
EXAMPLE 13.22
<html>
<head><title>Event Object and Incompatible Browsers</title>
<script>
1
function whichEvent(e) { // passing e is for Firefox
2
if(!e){ var e=window.event ;} // Microsoft IE
Continues
 
 
Search WWH ::




Custom Search