18

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.

leonheess
  • 16,068
  • 14
  • 77
  • 112
user3517251
  • 189
  • 1
  • 1
  • 4
  • 3
    possible duplicate of [How do I disable right click on my web page?](http://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page) – kol Apr 10 '14 at 15:43
  • Is the video player being loaded dynamically into the DOM? – user2521439 Apr 10 '14 at 17:23

7 Answers7

60

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:

<video oncontextmenu="return false;" id="my-video-player" width="854" height="480" controls autoplay>
  <source src="https://example.com/link-to-my-video.mp4" type="video/mp4">
</video>

You can also trigger it programatically such as:

document.querySelector('video').setAttribute('oncontextmenu', "return false;")
Anthony
  • 13,434
  • 14
  • 60
  • 80
jsherk
  • 6,128
  • 8
  • 51
  • 83
  • I just found that you can circumvent this by right clicking, holding the mouse button, moving the mouse slightly then releasing... somehow it then still opens the context menu!? – Sorcy May 12 '20 at 06:49
  • 1
    Note this _does_ require JavaScript! The `return false;` is JavaScript. If you disable JavaScript, the context menu appears as normal. – jameshfisher Jan 26 '21 at 17:07
19
 $(document).ready(function() {
    $("video").bind("contextmenu",function(){
        return false;
        });
 } );

This should disable right click on all the video elements in that page. Hope this helps.

Atul
  • 874
  • 1
  • 7
  • 17
  • I just tried and it still doesn't work. I tested with both Firefox and Chrome. Any ideas? – user3517251 Apr 10 '14 at 17:19
  • 1
    @user3517251 - is it happening only for **video** element, or all other elements. i mean, did this also not work `$(document).bind("contextmenu",function(){ return false; });` – Atul Apr 10 '14 at 17:32
  • this doesn't work either. it has no effect. perhaps something blocks it. any ideas what it could be? – user3517251 Apr 10 '14 at 17:39
  • 7
    I managed to get it to work by adding the following values in the DIV element of the Video.
    here's the video embedcode
    . It works great in Firefox, Chrome and IE.
    – user3517251 Apr 10 '14 at 18:06
  • ideally it should have worked ... a check you can do is whether jquery.js file was loaded or not. You can do it by placing- `alert("jquery loaded")` inside your `$(document).ready(function(){});` – Atul Apr 10 '14 at 18:10
4

I was recently trying to disable right click for video element. This code worked for me.

 document.querySelector("video").addEventListener("contextmenu", (event) => {
      event.preventDefault();
    });
Sudhir
  • 312
  • 1
  • 4
  • 10
4

For React use =>

onContextMenu={e => e.preventDefault()}

<video onContextMenu={e => e.preventDefault()}>
  <source src={`your_video_url`} type="video/mp4"/>
</video> 
1

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.

jQuery(document).ready(function(){
    jQuery('video').bind('contextmenu',function() { return false; });
});
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
bobo
  • 11
  • 1
1

It works fine for me.

$(document).bind("contextmenu",function(ev){
    if(ev.target.nodeName=='VIDEO')
    {
        return false;
    }
});
nacesprin
  • 392
  • 7
  • 16
0

Better to use onContextMenu={(e) => e.preventDefault()} under the video tag of HTML5.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103