views:

2276

answers:

4

Not that I'm trying to prevent 'View Source' or anything silly like that, but I'm making some custom context menus for certain elements.

EDIT: response to answers: I've tried this:

<a id="moo" href=''> </a>

<script type="text/javascript">
    var moo = document.getElementById('moo');

    function handler(event) {
        event = event || window.event;

        if (event.stopPropagation)
            event.stopPropagation();

        event.cancelBubble = true;
        return false;
    }

    moo.innerHTML = 'right-click here';

    moo.onclick = handler;
    moo.onmousedown = handler;
    moo.onmouseup = handler;
</script>
+3  A: 

Capture the onContextMenu event, and return false in the event handler.

You can also capture the click event and check which mouse button fired the event with event.button, in some browsers anyway.

Triptych
Glad this worked. You'll definitely want to check this on all your target browsers though.
Triptych
+2  A: 

Dark side-note - I've never seen a right-click script that would work on Opera, even if Opera is set to allow right-click intercepting (which is by default off).

Vilx-
yeah, I intend on having alternate-although-less-convenient ways of accessing the same actions for Opera
Jimmy
+2  A: 

If your page really relies on the fact that people won't be able to see that menu, you should know that modern browsers (for example Firefox) let the user decide if he really wants to disable it or not. So you have no guarantee at all that the menu would be really disabled.

Marc
+1  A: 

You can't rely on context menus because the user can deactivate it. Most websites want to use the feature to annoy the visitor.

stesch