Reputation: 143
I need to disable right-click on a hyperlink which is in a span. I need to disable only one link from the whole page. Any suggestions?
Upvotes: 14
Views: 17335
Reputation: 390
You can also use jQuery:
$(".myHyperlinks").contextmenu(function () { return false; });
Upvotes: 0
Reputation: 141
IN MVC:
@Html.ActionLink("print page", "myprint", "print", null, new { @oncontextmenu="return false;"})
Upvotes: 0
Reputation: 14345
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;
}
});
Upvotes: 2
Reputation: 4179
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>
Upvotes: 37
Reputation: 18797
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.
Upvotes: 8
Reputation: 2009
I have never seen one done through HTML (that does not imply it is not possible). However, JavaScript can help you here.
You can do something like:
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
Upvotes: 0