How to prevent Right Click option using jquery
Solution 1
$(document).ready(function() {
$("img").on("contextmenu",function(){
return false;
});
});
Working example: http://jsfiddle.net/vak9exyk/
Solution 2
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>
Solution 3
<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)
Solution 4
$(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
Solution 5
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
Related videos on Youtube
mymotherland
Eager to learn Live as if you were to die tomorrow. Learn as if you were to live forever! Let us continue learning as long as we live... only to add quality to the life we live! ஊக்கமது கைவிடேல் - எப்போதும் முயற்சியைக் கைவிடக்கூடாது.
Updated on June 23, 2021Comments
-
mymotherland almost 3 years
Is it possible to prevent RIGHT CLICK option for IMAGES which we use in web page.
-
Daniel Sloof about 13 yearsI 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.
-
nfechner about 13 yearsAre 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.
-
Wesley Murch about 13 yearsMight 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).
-
Demian Brecht about 13 yearsAs others have already pointed out here, sure it is. However, why? All someone has to do is disable Javascript, or view your page source to find the location, or use any number of more complex methods to get the image.
-
Wesley Murch about 13 years...or the highly unorthodox and very complex "Print Screen" :)
-
willdanceforfun about 13 yearsAlso, on mac, you can just drag the image off the page wherever you like. You might want to look at having the image not inside an <img> tag and put it inside a <div> as a css background to add further difficulty for the average user.
-
happy about 10 yearsIf protecting image is the reason a real solution would be to use watermark. Is it another X Y problem?
-
Morvael over 7 yearsI 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.
-
Black over 6 yearsMost people do not even know how to bypass this block.
-
-
mymotherland about 13 yearsThanks anu,like i said above... this prevent right click for whole page,i need only for image...
-
willdanceforfun about 13 yearshere's how i've extended it to add further difficulty for mac users jsfiddle.net/codedog/uKmDX
-
Peeter about 13 yearsI honestly don't like the idea of preventing right-clicks on an image.
-
willdanceforfun about 13 yearsneither. we are now enablers of a worse interweb
-
willdanceforfun about 13 yearsi just didn't know you could bind contextmenu :)
-
Richard Neil Ilagan about 13 yearsBad interweb design aside, this is one cool lesson for today. Didn't know you could do that. +1 :D
-
nspace over 9 yearsWorks perfect thanks. I am using this on a full screen browser in kiosk mode which I feel is a legit reason to disable to context menu.
-
Richard de Wit over 9 years@willdanceforfun - Your fiddle is broken. Also dont use a white image on a white background :P Updated fiddle. And OP:
.bind()
is depricated, use.on()
instead -
Richard de Wit over 9 years
$(this)
was what I was looking for, thanks! Also.bind()
is depricated, use.on()
instead -
Martin Spamer about 9 yearsDownvoted for promoting web Browser Hijacking and standards breaking actions.
-
Peeter about 9 yearsCould you explain a bit @MartinSpamer ?
-
ʰᵈˑ about 9 years@MartinSpamer Someone explained why they might need it. "I am using this on a full screen browser in kiosk mode which I feel is a legit reason to disable to context menu"... It's not promoting it either, it's answering a question. (Although I agree that disabling a right click is not advised)
-
Sannu about 9 yearsThank you.$(this) solved my case as well. Great answer!
-
chris.fy over 7 yearsFor me, onmousedown event wasn't firing when hovering over an image on IE - I found that the only way to get an alert to appear was through this event.
-
Sedat Kumcu over 6 yearsWorks fine. Thanks for this tip
-
Barath Kumar about 5 yearsThank you. you can find some slimier article in the following link. thedevelopertips.com