-2

I want to achieve disable the first right click and then re-enable right click on the same page.

<script type="text/javascript">
  var elements = document.getElementsByTagName("body");
  for(var id = 0; id < elements.length; ++id) { 
    elements[id].oncontextmenu = null; 
  }
</script>
/* this code will re-enable the right click */
<html>
  <body oncontextmenu="return false;">
    
    <!-- This will disable right click -->
    
  </body>
</html>
Sudarshan
  • 1
  • 3
  • Cases when you want to enable and disable the right click??? – Vibhesh Kaul Aug 27 '15 at 11:45
  • 2
    You seem to have forgotten to ask a question and your question is formatted in a wrong way. You'll probably get downvotes for this. – kemicofa ghost Aug 27 '15 at 11:45
  • i have gallery page on that i want to disable right click when user click on any image it will go to login page after login i want eturn to gallery page with right click re-enable. – Sudarshan Aug 27 '15 at 11:56

1 Answers1

0

Any user can prevent JavaScript from blocking the context menu, but the basic idea is use a boolean flag inside a function to enable and disable it.

var isDisableRightClick = true;
document.body.addEventListener("contextmenu", function (evt) { 
    if (isDisableRightClick) evt.preventDefault();
});

document.getElementById("x").addEventListener("click", function(){
    isDisableRightClick = !isDisableRightClick;
});
<button id="x">toggle</button>
<p>Filler</p><p>Filler</p><p>Filler</p><p>Filler</p><p>Filler</p>
epascarello
  • 204,599
  • 20
  • 195
  • 236