是否有可能禁用iframe上的右键单击?我知道如果iframe中的文件位于同一域中,则可能是可能的,但我想知道如果框架中的文件来自外部站点,是否可以完成此操作?
谢谢
是否有可能禁用iframe上的右键单击?我知道如果iframe中的文件位于同一域中,则可能是可能的,但我想知道如果框架中的文件来自外部站点,是否可以完成此操作?
谢谢
实际上,你无法真正禁用上下文菜单。 你只能构建脆弱的屏障来防止人们调用它。但是,这是外部iframe的事实只会加剧问题。不好意思,你无法阻止用户在你的iframe上激活上下文菜单。
这段代码在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>
使用jQuery:
$("#iframe-id").on("load", function () {
$(this).contents().bind('contextmenu', function () {
return false;
})
});
是的,可以做到以下所有的事情:禁用下载、打印、保存、屏幕截图和任何键盘上的按钮,以提供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文件提供任何类型的安全性,甚至可以阻止任何键盘按键以进行保护。
执行步骤
如果您创建一个 div
,并在其中添加 z-index
,那么这是可能的。
在配置了 width
和 height
之后,添加 filter:alpha(opacity=50); opacity:0.5;
,然后将代码放入您的网站中以阻止右键单击...