Cancelling and Redirecting Events in NN6+ : Browser Event « Window Browser « JavaScript DHTML






Cancelling and Redirecting Events in NN6+

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

John Wiley & Sons CopyRight 2001
*/


<HTML onClick="revealEvent('HTML', event)">
<HEAD>
<TITLE>Event Cancelling & Redirecting</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// display alert with event object info
function revealEvent(elem, evt) {
    var msg = "Event (from " + evt.target.tagName + " at "
    msg += evt.clientX + "," + evt.clientY + ") is now at the "
    msg += elem + " element."
    alert(msg)
}
function init() {
    document.onclick = docEvent
    document.body.onclick = docBodEvent

}
function docEvent(evt) {
    revealEvent("document", evt)
}
function docBodEvent(evt) {
    revealEvent("BODY", evt)
}
function buttonEvent(form, evt) {
    revealEvent("BUTTON", evt)
    // redirect if checked
    if (form.redirect.checked) {
        document.body.dispatchEvent(evt)
    }
    // cancel if checked
    if (form.bubbleCancelState.checked) {
        evt.stopPropagation()
    }
}
</SCRIPT>
</HEAD>
<BODY onLoad="init()">
<H1>Event Cancelling & Redirecting</H1>
<HR>
<FORM onClick="revealEvent('FORM', event)">
<P><BUTTON NAME="main1" onClick="buttonEvent(this.form, event)">
Button 'main1'
</BUTTON></P>
<P><INPUT TYPE="checkbox" NAME="bubbleCancelState"
 onClick="event.stopPropagation()">Cancel Bubbling at BUTTON<BR>
<INPUT TYPE="checkbox" NAME="redirect" onClick="event.stopPropagation()">
Redirect Event to BODY</P>
</FORM>
</BODY>
</HTML>

           
       








Related examples in the same category

1.Using the History Object to Navigate
2.Handling onBlur and onFocus in Frames
3. IE4+ Event Coordinate Properties
4.Using the srcElement property
5. NN4 Event Capture and Release 1
6.Document and Layer Event Capture and Release
7.NN4 Capture, Release, and Route Events
8.NN4 Redirecting Events
9. Event Bubbling Demonstration
10.Cancelling and Redirecting Events in IE5.5+
11.NN6 Event Capture and Bubble
12.Preventing Bubble and Capture