Search code examples
javascripthtmlpdfiframefrontend

how to block right mouse click on iframe


How do I block the right mouse button click on top of the iframe, I tried to use the oncontextmenu="return false" in the iframe tag but it didn't work, in my case I want to change the right button on top of the pdf that I do this being a example inside the iframe, here is an example inside my code:

<html>
<head>
    <title>block the right mouse</title>
</head>
<body >
    <iframe oncontextmenu="return false" id="iframepdf" src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0" width="500px" height="600px" type="application/pdf"></iframe>
</body>

Solution

  • This is a complete code to disable the right click

    <html>
      <head>
        <title>Disable Context Menu</title>
        <script type="text/jscript">
          function disableContextMenu()
          {
            window.frames["fraDisabled"].document.oncontextmenu = function() 
             { 
                alert("No way!"); 
                return false;
             };   
          }  
        </script>
      </head>
      <body bgcolor="#FFFFFF" onload="disableContextMenu();" oncontextmenu="return 
       false">
        <iframe id="fraDisabled" width="528" height="473" src="local_file.html" 
           onload="disableContextMenu();" onMyLoad="disableContextMenu();"> 
         </iframe>
      </body>
    </html>
    

    I Hope this is useful....