How to block right mouse click on iframe?

Member

by orpha , in category: Javascript , a month ago

How to block right mouse click on iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , a month 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.