Tanu
Tanu

Reputation: 143

How to disable right-click on a hyperlink in html

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

Answers (7)

Stephen Anderson
Stephen Anderson

Reputation: 390

You can also use jQuery:

    $(".myHyperlinks").contextmenu(function () { return false; });

Upvotes: 0

pankaj jain
pankaj jain

Reputation: 141

IN MVC:

@Html.ActionLink("print page", "myprint", "print", null, new { @oncontextmenu="return false;"})

Upvotes: 0

Brett Zamir
Brett Zamir

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

AjayR
AjayR

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

Amit
Amit

Reputation: 22076

Try this oncontextmenu="return false;"

Upvotes: 0

Kamyar
Kamyar

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

check123
check123

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

Related Questions