Disable the Right Button To prevent Users to show Web Page Code
In this snippet we are going to see how to block the right mouse button to prevent the user from taking out the context menu. This is good because in this context menu appears, among other options, to show source code.
Now that we can think that by deleting this context menu we prevent our source code from being inspected, although there are other ways that our source code can be stolen. However, there are many obstacles to our source code being inspected.
One of the worst things about the Internet is the discrepancies between different browsers. In part understandable by the slowness in the standardization of the committees (slowness compared to the speed of development of the network).
Although DOM Level2 Events tries to standardize event control. It is not possible today to make a single script for different browsers. And in all this embolado, Opera does not offer the support of cancellation of events and promotes the use of the contextual menu obtained by the right button. A mess.
If one thing is still more or less clear, although the subject press and release a button we could make a monologue, is that events can be captured with onmousedown. However, this may not prevent the browser from launching the context menu. It's not too much to stop to the context menu with the oncontextmenu attribute. To which, we only need to give a false value.
document.onmousedown=anularBotonDerecho; document.oncontextmenu=new Function("return false");
When calculating the pressed button we have two alternatives. Internet Explorer browsers are still based on the property event.button and the value 2 (right button).
if (event.button == 2) { alert("Right Button Clicked"); }
For its part, Firefox and Chrome rely on the property which and its values 2 and 3.
if (e.which == 3 || e.which == 2) { alert("Right Button Clicked"); }
In addition to validating the button pressed, we will add in the validation the browser on which we operate. In this case we will rely on the property Navigator.appName
function stopRightButton(e) { if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)){ alert(sMessage); return false; } else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2)) { alert(sMessage); } }