I’m using an HTML5 video player on my website and I want to disable right-clicking on all my videos.
I tried using this code and it doesn’t work:
<script type="text/javascript">
$(document).ready(function(){
$('#videoElementID').bind('contextmenu',function() { return false; });
});
</script>
I’m using WordPress and placed the function above in the header.php
file.
How can I check what is the #videoElementID
name for my player? I tried with all the DIV elements on the page and it still doesn’t work. I know this won’t prevent users to download my videos but I really need to make this work.
My favorite method which is quick and easy and does not require javascript is to add the
oncontextmenu="return false;"
to the video tag.So something like this:
You can also trigger it programatically such as:
This should disable right click on all the video elements in that page. Hope this helps.
I was recently trying to disable right click for video element. This code worked for me.
For React use =>
The right click menu is a function of the web browser. To disable it, you can try to add the following JavaScript to the head section of your web page, just before the tag.
It works fine for me.
Better to use
onContextMenu={(e) => e.preventDefault()}
under the video tag of HTML5.