Javascript 是否可以禁用 iframe 上的右键单击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:33:53  来源:igfitidea点击:

Is it possible to disable right click on an iframe?

javascriptiframe

提问by manraj82

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

是否可以禁用 iframe 上的右键单击?我知道如果 iframe 中的文件驻留在同一个域中是可能的,但是我想知道如果框架中的文件来自外部站点是否可以完成?

thanks

谢谢

回答by Sampson

You can't really disable the context menu to begin with. You can only construct fragile barricades to keep people from invoking it. But the fact that this is an external iframe only compounds the issue. No, you can't keep the users from activating the context menu on your iframe. Sorry.

您无法真正禁用上下文菜单。你只能建造脆弱的路障来阻止人们调用它。但这是一个外部 iframe 的事实只会使问题更加复杂。不,您不能阻止用户激活 iframe 上的上下文菜单。对不起。

回答by Dedrick

works on IE to disable right click on Iframe but the problem is it does not work with external websites ,,, iframed file must be at the same domain ... take a look on it

适用于 IE 以禁用 iframe 上的右键单击,但问题是它不适用于外部网站

<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>

回答by Andy E

No, it's not possible if it's on an external domain. A mouse click or any other event starts at the first, topmost element it fires on and then works its way back up the chain of elements (unless propagation is stopped). If you tried to stop it at the containing document it will have already fired on the relevant elements of the child document.

不,如果它在外部域上是不可能的。鼠标单击或任何其他事件从它触发的第一个最顶层元素开始,然后沿元素链返回(除非传播停止)。如果您试图在包含文档处停止它,它将已经在子文档的相关元素上触发。

回答by Hymanson

It's possible, if you create a div, and into this Divyou have to add z-index.

有可能,如果您创建一个div, 并且Div您必须在其中添加z-index.

After configuring width, height, add the filter:alpha(opacity=50); opacity:0.5;so, after that, you put a conde into your site blocking the right click...

配置widthheight,添加filter:alpha(opacity=50); opacity:0.5;so,然后,您将一个conde放入您的站点以阻止右键单击...

回答by Tejas Jadhav

yes, it is possible to do all following things: disable download, print, save, printscreen, and any button from keyboard to provide security for PDF.

是的,可以执行以下所有操作:禁用下载、打印、保存、打印屏幕和键盘上的任何按钮以提供 PDF 的安全性。

follow my project.....

关注我的项目.....

1 . Install server to run php files ( else use usb portable server ) 2. Create "Pdf_Files" folder in your project and paste your PDF files in it. 3.download pdf.js project 4. write the following codes...

1 . 安装服务器以运行 php 文件(否则使用 USB 便携式服务器) 2. 在您的项目中创建“Pdf_Files”文件夹并将您的 PDF 文件粘贴到其中。3.下载pdf.js项目 4.编写如下代码...

blocked.js

阻塞.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

我的Iframe.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>

using this full project you can gives any kind of security for your PDF file even you can block any key keyboard for protection purpose.

使用这个完整的项目,您可以为您的 PDF 文件提供任何类型的安全性,即使您可以出于保护目的阻止任何键盘。

steps to perform

执行步骤

  1. Start server
  2. Start Web-Browser & enter Url -" localhost:"port"/test.php "
  3. Add any pdf file in this folder " Pdf_Files " folder
  4. Refresh browser
  5. Added files autometicaly fetch on browser window with name .
  1. 启动服务器
  2. 启动 Web 浏览器并输入 URL -" localhost:"port"/test.php "
  3. 在此文件夹中添加任何 pdf 文件“ Pdf_Files ”文件夹
  4. 刷新浏览器
  5. 在浏览器窗口中添加文件自动获取名称。