04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: Events<br />

contextmenu event to specifically indicate when a context menu is about to be displayed, allowing<br />

developers to cancel the default context menu and provide their own.<br />

The contextmenu event bubbles, so a single event handler can be assigned to a document that handles<br />

all such events for the page. The target of the event is the element that was acted on. This event can be<br />

canceled in all browsers, using event.preventDefault() in DOM - compliant browsers and setting<br />

event.returnValue to false in IE. The contextmenu event is considered a mouse event and so has<br />

all of the properties related to the cursor position. Typically, a custom context menu is displayed using<br />

an oncontextmenu event handler and hidden again using the onclick event handler. Consider the<br />

following HTML page:<br />

< html ><br />

< head ><br />

< title > ContextMenu Event Example < /title ><br />

< /head ><br />

< body ><br />

< div id=”myDiv” > Right click or Ctrl+click me to get a custom context menu. Click<br />

anywhere else to get the default context menu. < /div ><br />

< ul id=”myMenu” style=”position:absolute;visibility:hidden;background-color:<br />

silver” ><br />

< li > < a href=”http://www.nczonline.net” > Nicholas ’ site < /a > < /li ><br />

< li > < a href=”http://www.wrox.com” > Wrox site < /a > < /li ><br />

< li > < a href=”http://www.yahoo.com” > Yahoo! < /a > < /li ><br />

< /ul ><br />

< /body ><br />

< /html ><br />

In this code, a < div > is created that has a custom context menu. The < ul > element serves as the custom<br />

context menu and is initially hidden. The JavaScript to make this example work is as follows:<br />

EventUtil.addHandler(window, “load”, function(event){<br />

var div = document.getElementById(“myDiv”);<br />

});<br />

EventUtil.addHandler(div, “contextmenu”, function(event){<br />

event = EventUtil.getEvent(event);<br />

EventUtil.preventDefault(event);<br />

});<br />

var menu = document.getElementById(“myMenu”);<br />

menu.style.left = event.clientX + “px”;<br />

menu.style.top = event.clientY + “px”;<br />

menu.style.visibility = “visible”;<br />

EventUtil.addHandler(document, “click”, function(event){<br />

document.getElementById(“myMenu”).style.visibility = “hidden”;<br />

});<br />

Here, an oncontextmenu event handler is defined for the < div > . The event handler begins by canceling<br />

the default behavior, ensuring that the browser ’ s context menu won ’ t be displayed. Next, the < ul ><br />

element is placed into position based on the clientX and clientY properties of the event object. The<br />

last step is to show the menu by setting its visibility to “ visible ” . An onclick event handler is<br />

408

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!