Javascript/jQuery – disable right click

Right click can be disable by attaching a even listener to contextmenu event. It can be done using jQuery or vanilla Javascript.

disable right click using jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<div id="count1">Hello world</div>

<script type="text/javascript">
$(document).on("contextmenu", function(e) {
  console.log("right click disabled");
  return false;
});
</script>
refresh

Here is the log message we can see in console when someone right clicks in the document.
javascript-disable-right-click-console-message

disable right click using vanilla Javascript

<div id="count1">Hello world</div>

<script type="text/javascript">
document.addEventListener("contextmenu", function(e) {
  console.log("right click disabled");
  e.preventDefault();
  return false;
});
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments