I am looking for a solution to disable right click over a specific div. I have this code that does this, but it also display an alert (i fixed this) and it works on the whole page.
<script language="JavaScript">
<!--
//Disable right mouse click Script
//By Oscar Frank
//For full source code, visit http://www.oscarmini.com
var message="";
///////////////////////////////////
function clickIE4(){
if (event.button==2){
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("return false")
// -->
</script>
Can someone help me make this work only one or more specific divs? Thanks! I am a noob.
On the div that you are trying to disable right click events, you can just add this:
oncontextmenu="return false;"
Example:
<div oncontextmenu="return false;">This div won't have the right click menu</div>
You have to add an event on the specific div ( you can get the div by id document.getElementById ) and add an event listener for contextmenu. Example:
document.getElementById("divId").addEventListener("contextmenu ", function(){
console.log("Right Click");
return false;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With