Javascript 如何禁用右键单击 IFRAME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26583033/
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
How to disable right click on IFRAME
提问by Lax
I have a document content displayed on IFrame in MVC web application. The content should not be copied and printed . I tried to disable right click using two functions style="pointer-events:none;" oncontextmenu="return false" for Iframe, which is working fine. But on right click, the pop up with 'View Frame Source', 'View Source' are displaying. How can I restrict this.! Also, how to restrict the print screen option. I know there are other utilities from where anybody can capture data. But the client wants to restrict the print screen option.
我在 MVC Web 应用程序的 IFrame 上显示了一个文档内容。不应复制和打印内容。我尝试使用两个函数 style="pointer-events:none;" 禁用右键单击 iframe 的 oncontextmenu="return false" 工作正常。但是在右键单击时,会显示“查看框架源”、“查看源”的弹出窗口。我怎么能限制这个。!另外,如何限制打印屏幕选项。我知道还有其他实用程序可供任何人捕获数据。但是客户想要限制打印屏幕选项。
<script lang=JavaScript>
function clickIE() {
if (document.all) {
return false;
}
}
function clickNS(e) {
if (document.layers || (document.getElementById && !document.all)) {
if (e.which == 2 || e.which == 3) {
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = clickNS;`enter code here`
}
else {
document.onmouseup = clickNS;
document.oncontextmenu = clickIE;
}
document.oncontextmenu = new Function("return false")
<body oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false" >
<div id="div1" style="background-color:Red; height:120px">
<iframe id="id1" src="" name="I1" scrolling="no" height="100%" width="100%" marginwidth ="0" marginheight="0" onload="disableContextMenu();" style="pointer-events:none;" />
</div>
Please Any help appreciated.. !!
请任何帮助表示赞赏.. !!
回答by jollelj
回答by Rajath Ramachandran
We can't just disable right click on the iframe. Because of the iframe content is loading from another source so our code will not work on it. Here I found a solution which is the only option we have.
我们不能只是禁用 iframe 上的右键单击。因为 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"></iframe>
<div style="width:528px;height:473px;background-color:transparent;position:absolute;top:0px;">
</body>
</html>
回答by Dave
Your question is a little confusing as the title is about right clicking, yet the bddy of the question is about copying and pasting and about using the print screen button. Whilst you can do some things with the right click button (already answered by other posts and well documented) generally your question is how to prevent people accessing the code/content or taking a print out of your content.
您的问题有点令人困惑,因为标题是关于右键单击的,但问题的主题是关于复制和粘贴以及使用打印屏幕按钮。虽然您可以使用右键单击按钮执行某些操作(已由其他帖子回答并有详细记录),但通常您的问题是如何防止人们访问代码/内容或打印出您的内容。
This isn't possible. Whilst you can make it more tricky for some users, it will never succeed against those who are determined enough.
这是不可能的。虽然您可以让某些用户更棘手,但对于那些足够坚定的人来说,它永远不会成功。
First of, even if you (somehow) disabled the print screen button on the keyboard, there are many screen capture programs out there... And I can't see how it will (ever) be possible to detect another program doing this from within the limitations of website code.
首先,即使您(以某种方式)禁用了键盘上的打印屏幕按钮,那里也有许多屏幕捕获程序......而且我无法看到(永远)如何检测到另一个程序执行此操作在网站代码的限制内。
Any javascript solution can fail, they can turn off javascript.
任何 javascript 解决方案都可能失败,他们可以关闭 javascript。
Even if you managed to prevent some one from viewing the source code and copying the HTML, some one could just scrape the content direct from the site.
即使您设法阻止某些人查看源代码和复制 HTML,某些人也可以直接从网站上抓取内容。
I have a friend who is a graphic designer and he wanted to do this (disable people copying images in this case). I told him not to bother, if they want to take the content you put into the public domain, they will. A water mark may help but only in some situations. Personally, I'd give up on this task and just accept it, and focus on more interesting tasks.
我有一个平面设计师朋友,他想这样做(在这种情况下禁止人们复制图像)。我告诉他不要打扰,如果他们想把你放在公共领域的内容,他们会的。水印可能会有所帮助,但仅限于某些情况。就个人而言,我会放弃这项任务并接受它,专注于更有趣的任务。
回答by Neo
回答by Gaurav Lodha
This worked for me fine:
这对我来说很好:
window.frames["your_iframe_id"].contentDocument.oncontextmenu = function(){
return false;
};
回答by boycod3
window.frames["your_iframe_id"].document.oncontextmenu = function(){ return false; };


