views:

174

answers:

1

in Firefox I used the document.onclick event and then checked if it was a right click, and If i right clicked everything went as expected. But in Chrome, Opera and IE8, if I right click the document.onclick doesn't fire.

I want to have a custom context menu for img elements. How do I go about this?

A: 

The right-click invokes the context menu within most standard browsers; therefore, you can use the "oncontextmenu" listener to handle right-click events. The listener should return false if you do not want it to display the standard browser context menu after invoking your JS code.

Here's some sample html that handles left and right clicks on an image.

<html>
<head>
    <script type="text/javascript">
        function handleRightClick() {
            alert("Got right click!");
        };

        function handleLeftClick() {
            alert("Got left click!");
        };
    </script
</head>
<body>
    <img src="http://thefuturebuzz.com/pics/the-matrix.jpg" onclick="handleLeftClick(this);" oncontextmenu="handleRightClick(this); return false;" />
</body>
</html>

For more information, check out http://www.w3schools.com/html5/html5_ref_eventattributes.asp

brianghig