subanki
subanki

Reputation: 1429

disable/enable right click on a particular part of the html body

i want to disable right click on the webpage but i want enable right click on textarea. Hey wat is this provide answers dont post lot of comments on right click (lol). i dont care if people would see my source code, thats nt the point ... i just want to know how one can enable right click only in the textarea while disabling the rest

so any1 here know the javascript function that would perform the job ??

is the below code possible ??

<html>
<head>
<title>  Your Title  </title>
</head>
<body oncontextmenu="return false;">
<textarea  oncontextmenu="return true;">


</textarea>
</body>
</html>

-thanx in advance

-miss subanki

Upvotes: 9

Views: 17934

Answers (7)

MiKhatri
MiKhatri

Reputation: 11

document.oncontextmenu = function(e) {
    var el = window.event.srcElement || e.target;
    var tp = el.tagName || '';
    if ( tp.toLowerCase() == 'input' || tp.toLowerCase() == 'select' || tp.toLowerCase() == 'textarea' )
    {
        return true;
    }
    else
    {
        return false;
    }
}

Upvotes: 0

Thar Htoo
Thar Htoo

Reputation: 11

let divs = $("div");
divs.each(function(i, v) {
  $(v).on("contextmenu", function() {
    return false;
  })
})
$(".no").off("contextmenu");
body {
  background: black;
}

.yes {
  width: 100px;
  height: 100px;
  background: tomato;
  text-align: center;
}

.no {
  width: 100px;
  height: 100px;
  background: cyan;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="yes">No Click</div>
<div class="no"></div>
<div class="yes">No Click</div>
<div class="no"></div>
<div class="yes">No Click</div>

The off() method removes event handlers that were attached with on(). jsfiddle => https://jsfiddle.net/dare444/we91t5gd/183/

Upvotes: 1

user3531155
user3531155

Reputation: 439

I have found one solution:

document.superListener = document.addEventListener;
document.addEventListener = function(type, listener, useCapture){
if(type != 'contextmenu')
    document.superListener(type, listener, !!useCapture);
};

from here: https://stackoverflow.com/a/3009161/3649420

Upvotes: 1

subanki
subanki

Reputation: 1429

To enable right click on a particular element on the body while disabling the right click on the rest of the body (in html), you wil have to put the required element (whose right click you want to enable) into an iframe. And disable the right click on main body like this....

Main Body

<html>
<head>
<title>Your Title</title>
</head>
<body  oncontextmenu="return false;">
<iframe src="frame1.html">
</iframe>
</body>
</html>

frame1.html

<html>
<body>
<textarea> Your text, u can right click here </textarea>
</body>
</html>

if anyone else has a better answer please post it here, thanx everyone.

Upvotes: 4

qw3n
qw3n

Reputation: 6334

http://www.quirksmode.org/js/events_properties.html#button has probably all the information you need. You get the click event and test to see which keycode it is. Then choose to return false or true depending on where the click came from.

Upvotes: 1

Roman
Roman

Reputation: 10403

You can disable the right click using javascript to keep the honest people honest. But the not so honest people can easily reverse this. If you are interested read on "oncontextmenu" property of html elements.

Upvotes: 0

Ashley
Ashley

Reputation: 5947

What about: http://www.dynamicdrive.com/dynamicindex9/noright2.htm

But there's not much point disabling right click, it's easy to bypass and get content.

Upvotes: 1

Related Questions