Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable right-click context-menu in JavaScript [duplicate]

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>
like image 238
Jimmy Avatar asked Dec 19 '08 18:12

Jimmy


People also ask

How do I disable default right-click menu?

Inside the event listener, we prevent the default browser behavior by calling the ev. preventDefault() method. This will prevent the browser from displaying the right-click menu.

How do you disable mouse right click in react JS?

To override the default browser right-click menu, the first step is to prevent the default right-click behavior. This can be done by listening to the contextmenu event and using the preventDefault() method on the event. Next, capture the current (x,y) position, and render your component at that coordinate on the page.


1 Answers

If you don't care about alerting the user with a message every time they try to right click, try adding this to your body tag

<body oncontextmenu="return false;">

This will block all access to the context menu (not just from the right mouse button but from the keyboard as well)

However, there really is no point adding a right click disabler. Anyone with basic browser knowledge can view the source and extract the information they need.

like image 132
Omar Wagih Avatar answered Oct 01 '22 06:10

Omar Wagih