3

I have the following element that I would like to prevent from being downloaded by disabling the right click.

<iframe src={TEST + "#toolbar=0"} width="100%" height="800px" 
onMouseDown={(e)=>e.preventDefault()} onContextMenu={(e)=>e.preventDefault()}/> 

Unfortunately, when I right-click, it still brings up the context menu. Any idea why?

Kayla Song
  • 63
  • 1
  • 6

1 Answers1

5

Use the contextmenu event inside componentDidMount() method of your component.

For example:

componentDidMount() {
  document.addEventListener('contextmenu', (e) => {
    e.preventDefault();
  });
};

This will prevent the context menu to be shown.

j3ff
  • 5,139
  • 7
  • 39
  • 50
  • This still didn't work. I think iframe might not be detecting any clicks because having the onClick function in iframe to a console log doesn't print anything. Any idea how to fix this? – Kayla Song Apr 08 '20 at 22:30
  • Check this, you will find the answer: https://stackoverflow.com/questions/16792953/onclick-function-doesnt-fire-on-iframe – Mohammed-yassin HAMDAOUI Apr 15 '20 at 15:30