Is it possible to prevent RIGHT CLICK option for IMAGES which we use in web page.
-
8I am normally annoyed by people questioning my motives instead of just answering the damn question, but do you really want to be hijacking a user's right mouse click? If they want to get your images they will get it one way or the other... Firebug or just looking at the source would be enough to get direct link to any of your images.Daniel– Daniel2011-04-11 07:32:27 +00:00Commented Apr 11, 2011 at 7:32
-
2Are you trying to prevent someone from downloading your images? In that case you can pretty much forget about it, because in order to seem the image in a browser is has to be copied to the client and will in most cases be stored somewhere on your disk. Also most browsers include a config option to prevent manipulation of context menues: Example for Firefox.nfechner– nfechner2011-04-11 07:32:31 +00:00Commented Apr 11, 2011 at 7:32
-
2Might be worth mentioning WHY you are attempting to disable right click. If you think you are protecting your images, think again - you are only deterring the computer illiterates (which does have its merit).No Results Found– No Results Found2011-04-11 07:33:09 +00:00Commented Apr 11, 2011 at 7:33
-
3If protecting image is the reason a real solution would be to use watermark. Is it another X Y problem?happy– happy2014-03-15 03:03:24 +00:00Commented Mar 15, 2014 at 3:03
-
5I wish people would get off their high horses here, there are innumerable reasons for wanting to do this other than just stopping people downloading the image. If the OP wants to do it, then in the spirit of Q&A then if you know how answer the question. If the OP had said "so users can't download the image" after answering go on to tell them why it's not a complete solution.Morvael– Morvael2016-10-06 16:11:06 +00:00Commented Oct 6, 2016 at 16:11
16 Answers
$(document).ready(function() {
$("img").on("contextmenu",function(){
return false;
});
});
Working example: http://jsfiddle.net/vak9exyk/
10 Comments
I think this should help. Trick is to bind the contextmenu event.
<script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});
</script>
2 Comments
$(this) was what I was looking for, thanks! Also .bind() is depricated, use .on() instead<body oncontextmenu="return false" onselectstart="return false" ondragstart="return false" >
Set these attributes in your selected tag
See here Working Example - https://codepen.io/Developer_Amit/pen/drYMMv
No Need JQuery (like)
Comments
$(document).ready(function() {
$(document)[0].oncontextmenu = function() { return false; }
$(document).mousedown(function(e) {
if( e.button == 2 ) {
alert('Sorry, this functionality is disabled!');
return false;
} else {
return true;
}
});
});
If you want to disable it only on image click the instead of $(document).mousedown use $("#yourimage").mousedown
1 Comment
The following code will disable mouse right click from full page.
$(document).ready(function () {
$("body").on("contextmenu",function(e){
return false;
});
});
The full tutorial and working demo can be found from here - Disable mouse right click using jQuery
Comments
Here i have found some useful link, with live working example.
I have tried its working fine.
How to prevent Right Click option using jquery
$(document).bind("contextmenu", function (e) {
e.preventDefault();
alert("Right Click is Disabled");
});
2 Comments
Method 1:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
</script>
Method 2:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
e.preventDefault();
});
});
</script>
Comments
Here is a working example, the red links can't be right clicked anymore.
$("ul.someLinks1 a").each(function(i, obj) {
$(obj).on("contextmenu",function(){
return false;
});
$(obj).css("color", "red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="someLinks1">
<li><a href="www.google.de">google</a></li>
<li><a href="www.stackoverflow.de">stackoverflow</a></li>
<li><a href="www.test.de">test</a></li>
</ul>
<ul class="someLinks2">
<li><a href="www.foobar.de">foobar</a></li>
<li><a href="www.foo.de">foo</a></li>
<li><a href="www.bar.de">bar</a></li>
</ul>
Comments
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
</script>
</head>
<body>
<p>Right click is disabled on this page.</p>
</body>
</html>
Comments
You can also use the .contextmenu() shortcut method. For example:
$(document).ready(function() {
$("#logo").contextmenu(function(e){
return false;
});
});
body {
background-color: #FFF;
}
#logo {
background: url(http://stackoverflow.com/favicon.ico) no-repeat;
width: 182px;
height: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Try right-click on the logo.
<div id="logo"></div>
1 Comment
jQuery("#wpcf7-f608-p651-o1").on("contextmenu",function(e){
return false;
});
jQuery('.messagedata').keydown(function(event) {
if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {
//alert('thou. shalt. not. PASTE!');
event.preventDefault();
}
});
jQuery(document).on('keypress', '.messagedata', function (e) {
const pressedKey = e.key;
let pressedKeyCode = e.charCode;
if ((pressedKeyCode >= 33 && pressedKeyCode <= 47) ||
(pressedKeyCode >= 58 && pressedKeyCode <= 64) ||
(pressedKeyCode >= 91 && pressedKeyCode <= 96) ||
(pressedKeyCode >= 123 && pressedKeyCode <= 126) )
{
e.preventDefault();
}
else {
}
});
jQuery('textarea').bind('input', function() {
var c = this.selectionStart,
r = /[^a-z0-9 .]/gi,
v = jQuery(this).val();
if(r.test(v)) {
jQuery(this).val(v.replace(r, ''));
c--;
}
this.setSelectionRange(c, c);
});
Comments
If you're looking into trying to disable the downloading/saving of your images, scripts won't stop that. You would probably have better luck doing this on a server configuration level (like modifying your .htaccess for example on Apache).
Try asking this on ServerFault.