How to block right mouse click on iframe?

Member

by orpha , in category: Javascript , 7 months ago

How to block right mouse click on iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 7 months ago

@orpha 

You can prevent right-clicking on an iframe by adding a script inside the iframe tag that disables the contextmenu event. Here's how you can do it:

1
2
3
4
5
6
7
<iframe id="myFrame" src="yourpage.html"></iframe>

<script>
  document.getElementById('myFrame').contentWindow.document.addEventListener('contextmenu', function (e) {
    e.preventDefault();
  });
</script>


This script will prevent the right-click context menu from appearing when the iframe is right-clicked.

Related Threads:

How to disable right click on iframe?
How to disable right-click in the iframe?
How to enable right click in p5.js?
How to enable middle-mouse clicks on iframe?
How to disable all mouse events except for hover in iframe?
How to block pop-up coming from iframe?