dev4web
7/28/2016 - 5:14 PM

disableContextMenu.md

If you're building a web app, it's good to be familiar with the different techniques to disable the right-click menu, AKA context menu. Of course, many would suggest this should never be done, but now that web apps are pushing more towards native behavior, I'm sure a lot of developers will be interested in doing this sort of thing.

Here I'm listening for the contextmenu event, then I use event.preventDefault() (like the old return false) to keep the menu from appearing. This disables the context menu whether it's accessed via mouse or keyboard.

To disable the context menu, the best technique is as follows:

window.addEventListener('contextmenu', function (e) {
    console.log('context menu disabled');
    e.preventDefault();
}, false);