9

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

subanki
  • 1,411
  • 10
  • 25
  • 48
  • 1
    nobody told you it's useless to forbid the right-click? it's absolute nonsense, and users can mimic the right-click! –  Jul 12 '10 at 17:22
  • why oh why would you want to do that? – gblazex Jul 12 '10 at 17:23
  • Friends , i am asking my doubts here just for my knowledge, i am not interested in implementing it. – subanki Jul 12 '10 at 17:26
  • 4
    My friend, rule `no. 1`: don't waste other people's time, `no. 2`: if you want to improve your knowledge, try to learn useful things. Like the ones that doesn't irritate users... – gblazex Jul 12 '10 at 17:29
  • 3
    you people are only giving comments on the topic , if you dont want to tell me the answer thats fine with me ..but dnt pass useless things in post – subanki Jul 12 '10 at 17:30
  • you know there is action and reaction. Useless topic breeds useless comments... surprised? – gblazex Jul 12 '10 at 17:40
  • @ galamblazs, please stop these useless posting here just to get back at me ....if u know the answer u tell me , if u dont then leave this post or come back when we find an answer....and about that up arrow besides ur earlier comment went to 2 becaz i just clicked there accidently to find out wat it is..(lol) – subanki Jul 12 '10 at 17:43
  • *"you people are only giving comments on the topic"* - I thought that's why there is a comment section... :) Damn I might've been wrong... – gblazex Jul 12 '10 at 18:11

6 Answers6

4

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.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
subanki
  • 1,411
  • 10
  • 25
  • 48
1

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

Community
  • 1
  • 1
user3531155
  • 439
  • 4
  • 11
1

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.

Ashley
  • 5,939
  • 9
  • 39
  • 82
  • i went to that site before posting here , but i couldnt figure it out how to enable right clck on textarea while keeping the right click in the body part disabled – subanki Jul 12 '10 at 17:37
1

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.

qw3n
  • 6,236
  • 6
  • 33
  • 62
1

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/

Thar Htoo
  • 11
  • 2
0

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.

Roman
  • 10,309
  • 17
  • 66
  • 101
  • I've actually gone to great lengths to get content that is protected by javascript walls, not because I wanted the content, but because I wanted the challenge. There are some really fun methods for hiding images, like hiding them behind tranparent images, or only downloading if they are accessed in the correct order. My favorite has to be, drawing the images using a series of canvas objects and style sheets so that images in the cache are scrambled. All these methods can be defeated though given someone that wants to kill a few hours. – Ethan Heilman Jul 12 '10 at 17:31
  • i dont hav problem with others finding my source code , can u show me hw it can be done ?? – subanki Jul 12 '10 at 17:31
  • "I've actually gone to great lengths to get content that is protected by javascript walls, not because I wanted the content, but because I wanted the challenge" ... cool i like tat attitude – subanki Jul 12 '10 at 17:33