Is it possible to disable right click on iframe?

Is it possible to disable right click on iframe? I know that it is possible if the file in the iframe is in the same domain, but I was wondering if it could be done if the file in the frame was from an external site?

thank

+3
source share
5 answers

In fact, you cannot disable the context menu. You can create fragile barricades so people don’t cause it . But the fact that this is an external iframe only exacerbates the problem. No, you cannot prevent users from activating the context menu on your iframe. Unfortunately.

+10
source

IE, Iframe, , -,, iframed ...

<html>
<head>
<title>Disable Context Menu</title>
<script type="text/jscript">
  function disableContextMenu()
  {
    window.frames["fraDisabled"].document.oncontextmenu = function(){alert("No way!"); return false;};   
    // Or use this
    // document.getElementById("fraDisabled").contentWindow.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>
+4

, , . , , , ( ). , .

0

, : , , , , PDF.

.....

1. php ( usb). 2. "Pdf_Files" PDF. 3. pdf.js 4. ...

blocked.js

$(function() //right click disabled
{
    $(this).bind('contextmenu',function()
    {
        alert("Function disabled");
        return false;
    })
});

function copyToClipboard() {
  var aux = document.createElement("input");
  aux.setAttribute("value", "Function Disabled.....");
  document.body.appendChild(aux);
  aux.select();
  document.execCommand("copy");
  document.body.removeChild(aux);
  alert("Print screen disabled.");
}

function blockPrint() {
  alert("Print is not allowed...");
}

 function PreSaveAction() { 
    alert("saving..");
 }

$(function()
{
    $(this).keyup(function(e){
      if(e.keyCode == 44 || e.keyCode == 137 ||e.KeyCode == 93 )
      //100 Save 137 SHift F10 93 RightClick 44 PrintScreen
      {
        copyToClipboard();
        return false;
      }
    })
}); 

//disable Ctrl + keys
document.onkeydown = function (e) {
    e = e || window.event;//Get event
    if (e.ctrlKey) {
        var c = e.which || e.keyCode;//Get key code
        switch (c) {
            case 83://Block Ctrl+S
            case 80 : //block Ctrl+P
            case 17 : //block Ctrl
            case 16 : //block Shift
                e.preventDefault();     
                e.stopPropagation();
                alert("key disabled");
            break;
        }
    }
};


$(window).focus(function() {
  $("body").show();
}).blur(function() {
  $("body").show();
});

function setClipBoardData(){ //override system function - make clipBoard null always
    setInterval("window.clipboardData.setData('text','')",20);
}
function blockError(){
    window.location.reload(true);
    return true;
} 

MyIframe.php

<html>
<head>
    <title> </title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="blocked.js"></script>
    <link rel="stylesheet" type="text/css" href="myStyle.css">
</head>

<body onbeforeprint="copyToClipboard()" >
<?php

    $file = './Pdf_Files/';

    if( isset($_REQUEST["path"] ) )
        $file .= $_REQUEST["path"];

    echo ' <iframe src="pdfjs/web/viewer.html?file=../../'. $file .'#toolbar=0&navpanes=0"  /> ';

?>

</body>
</html>

myStyle.css

@media print { * {  display: none; } } /* make print blank */

iframe {
    height: 100%;
    width:100%;
    padding:0;
    overflow-x: scroll;
    border:none;
    overflow-y: scroll;
}

/* disable selection highlighting, from https://stackoverflow.com/a/4407335 */
* {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
}

input[type="submit"] {  /* make submit btn as link */
    background:none!important;
    color:inherit;
    border:none; 
    padding:0!important;
    font: inherit;
    border-bottom:1px solid #444; 
    cursor: pointer;
}

test.php

<html>
<head>
<title>  </title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="blocked.js"></script>
<link rel="stylesheet" type="text/css" href="myStyle.css">

<body>
    <form method="post" action="MyIframe.php" >

    <table align="center" width="800px" cellspacing="20px" >
<?php

    $path = './Pdf_Files/';
    $count = 0;

    if( $handler = opendir( $path ) )   
    {
        while( false !== ($file = readdir($handler)))
        {
            if( strpos($file, '.pdf' ) !== false )          
            {
                if( $count++ % 2 == 0 ) //make cloumn count 2
                    echo '<tr>';

                echo '<td> * <input type="submit" name="path" value="'. $file .'" /> </td> ';
            }
        }
        closedir($handler);
    }

?>

    </table>
</form>

</body>
</html>

, PDF , .

  1. - Url - "localhost:" port "/test.php"
  2. PDF "Pdf_Files"
  3. autometicy fetch .
0

, div, div z-index.

After configuration width, heightadd filter:alpha(opacity=50); opacity:0.5;, after which you will place conde on your website, blocking the right click ...

-1
source

Source: https://habr.com/ru/post/1733030/


All Articles