How to disable right-click on a hyperlink in html

If you dont want to show the context menu on hyperlink, you can do so without doing anything to other part or even span where it exists. I tested in IE, Firefox and it works.

<a href="#" oncontextmenu="return false;"> Link </a>

This should work:

oncontextmenu=”return false;”     

Place it on any element you want to disable right click for.
Be aware that this causes bad user experience and users can disable it very easily.
Disclaimer: not tested.


If you don't want to pollute your HTML with inline events and you care about supporting IE < 9, you can use this lovely mess:

function addEvent (el, eventType, listener) {
    if (el.addEventListener) { // W3C-compliant
        el.addEventListener(eventType, listener, false);
    }
    else {// IE-specific
        el.attachEvent('on'+eventType, listener);
    }
}
addEvent(document.getElementById('myLinkID'), 'contextmenu', function (e) {
    if (e.preventDefault) { // W3C
        e.preventDefault();
    }
    else { // IE
        e.returnValue = false;
    }
});

Tags:

Html