在iframe上禁用右键点击是否可能?

3

是否有可能禁用iframe上的右键单击?我知道如果iframe中的文件位于同一域中,则可能是可能的,但我想知道如果框架中的文件来自外部站点,是否可以完成此操作?

谢谢


2
禁用右键菜单的网站总是让我想起那些试图隐藏宝贵资源的粗制滥造的网站! - Andy E
@Andy:我并不是想隐藏我的宝贵脚本,只是想防止用户刷新它...我知道人们仍然可以找到刷新的方法,但只是想看看我有哪些选择...无论如何,谢谢。 - manraj82
6个回答

9

4

这段代码在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>

这是一个不错的例子,Dedrick。 - ameya rote
这个例子对于包含PDF文件的iframe失败了,有没有办法在IE中停止它。 - ameya rote

1

使用jQuery:

$("#iframe-id").on("load", function () {
  $(this).contents().bind('contextmenu', function () {
    return false;
  })
});

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

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://dev59.com/pnRA5IYBdhLWcg3wyRF6#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. 启动服务器
  2. 启动Web浏览器并输入URL -“localhost:”端口“/test.php”
  3. 将任何PDF文件添加到此文件夹“Pdf_Files”文件夹中
  4. 刷新浏览器
  5. 添加的文件会自动在浏览器窗口上显示名称。

-1

如果您创建一个 div,并在其中添加 z-index,那么这是可能的。

在配置了 widthheight 之后,添加 filter:alpha(opacity=50); opacity:0.5;,然后将代码放入您的网站中以阻止右键单击...


你的网站屏蔽了右键点击,同时也屏蔽了所有点击事件! - Amin Ghaderi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接