Preventing the use of the Context menu
When looking for a method to prevent a visitor using the righ-click to make a copy of an image I came across code relating to the context menu.
This works for a right-click anywhere on the page. There are two alert() boxes, this can be simplified as the second is a fix for an earlier version of Microsoft I.E. (see below).
The JavaScript for no context menu
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
alert("You've tried to open context menu 1"); //here you draw your own menu
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function() {
alert("You've tried to open context menu 2");
window.event.returnValue = false;
});
}
The script on this page was taken from a post on Stackoverflow and interecepts ALL right clicks and displays an alert box.
Where I wanted to be able to do this was on "click" event on all the images on a page (images were opened in full resolution on a non-html page). I certainly don't want to disable ALL clicks, as I will probably lock the page, navigation etc.
The example on stackoverflow is interesting as it does mention the selective interuption for specific elements on the page.
I admit that I don't fully understand what is going on in the JavaScript. The second part with the attachEvent() method in the else is a "fix" for I.E.8 ?????