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>
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With