Javascript 在网站上禁用复制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8365272/
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
Disable Copying on a website
提问by Phillip Senn
I know that it's impossible to thwart the world's most advanced minds, but I'd like to put the slightest of barriers on my website to keep my students from copying text from it and posting that text as their answer. (If they hand type it, that's ok).
我知道阻止世界上最先进的思想是不可能的,但我想在我的网站上设置最轻微的障碍,以防止我的学生从中复制文本并将该文本作为他们的答案发布。(如果他们手动输入,那没关系)。
I'm just so afraid of JavaScript because of cross browser inconsistencies.
由于跨浏览器的不一致,我非常害怕 JavaScript。
Given that I have jQuery loaded and prefer to use jQuery whenever possible, how do I:
鉴于我已加载 jQuery 并尽可能使用 jQuery,我该如何:
- Disable Ctrl + c
- Disable Menu Edit Copy.
- 禁用 Ctrl + c
- 禁用菜单编辑复制。
采纳答案by Mark Robbins
If you have your texts in particular divs, you could put a transparent div on top of those divs. Secondly, you could make all your protected text dynamic, and inject it into the divs from javascript where is would exist in a coded form -- that would defeat a 'view-source'.
如果您有特定 div 中的文本,则可以在这些 div 之上放置一个透明 div。其次,您可以使所有受保护的文本动态化,并将其从 javascript 注入到 div 中,其中以编码形式存在——这将破坏“视图源”。
回答by Charming Prince
Its some how daunting to create a function that would do that, what you should target is, clearing the clipboard so even if, the user press Ctrl + C, nothing is copied into the clipboard, a simple function like this should do the trick :
创建一个可以做到这一点的函数是多么令人生畏,你应该瞄准的是,清除剪贴板,这样即使用户按下 Ctrl + C,没有任何东西被复制到剪贴板,像这样的简单函数应该可以解决问题:
<script language="javascript">
function clearData(){
window.clipboardData.setData('text','')
}
function cldata(){
if(clipboardData){
clipboardData.clearData();
}
}
setInterval("cldata();", 1000);
</script>
<body ondragstart="return false;" onselectstart="return false;" oncontextmenu="return false;" onload="clearData();" onblur="clearData();">
although this can still be defeated....
虽然这还是可以打败的……
回答by Amit Kumar Khare
Just add the following code right before closing </HEAD> tag of your web page:
只需在关闭网页的 </HEAD> 标记之前添加以下代码:
<script type="text/JavaScript">
function killCopy(e){
return false
}
function reEnable(){
return true
}
document.onselectstart=new Function ("return false")
if (window.sidebar){
document.onmousedown=killCopy
document.onclick=reEnable
}
</script>
回答by iDhavalVaja
<script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});
</script>
<script type="text/JavaScript">
function killCopy(e){ return false }
function reEnable(){ return true }
document.onselectstart=new Function ("return false");
if (window.sidebar)
{ document.onmousedown=killCopy;
document.onclick=reEnable; }
</script>
//By using above code you right click will be disabled as well as no one can copy your page content
//通过使用上面的代码,您的右键单击将被禁用,并且没有人可以复制您的页面内容
回答by jmarceli
I would suggest you to use:
我建议你使用:
<div oncopy="return false;">Here you have protected text</div>
Support for this method could be found here: http://help.dottoro.com/ljwexqxl.php
可以在此处找到对此方法的支持:http: //help.dottoro.com/ljwexqxl.php
It is simple and in my opinion sufficient against regular users. To be honest there is no option to fully prevent copying text. One can always use for example Chrome Developer Tools and copy even dynamically loaded text from there.
这很简单,在我看来足以对付普通用户。老实说,没有选项可以完全防止复制文本。人们总是可以使用例如 Chrome 开发人员工具,甚至可以从那里复制动态加载的文本。
For more effective protection you should place oncopy
in <body>
tag because otherwise it is possible to copy text by starting selection from outer <div>
.
为了获得更有效的保护,您应该放置oncopy
在<body>
标记中,否则可以通过从外部开始选择来复制文本<div>
。
回答by Faruque Ahamed Mollick
Selecting text, copy, the right click can be disabled on a web page easily using jQuery. Below is the simple jQuery code snippet which can do this task easily:
使用 jQuery 可以轻松地在网页上禁用选择文本、复制、右键单击。下面是可以轻松完成此任务的简单 jQuery 代码片段:
<script type="text/javascript">
// Disable right click on web page
$("html").on("contextmenu",function(e){
return false;
});
// Disable cut, copy and paste on web page
$('html').bind('cut copy paste', function (e) {
e.preventDefault();
});
</script>
Source: Disable right click, copy, cut on web page using jQuery
回答by gtiwari333
To achieve that you need to block mouse click and context menu click on your webpage.
为此,您需要阻止鼠标单击和上下文菜单单击您的网页。
Here is a sample code:
这是一个示例代码:
<script language="JavaScript1.2">
var msgpopup="COPYING CONTENT IS PROHIBITED";
function handle(){
if(toShowMessage== "1") alert(message);
if(closeSelf== "1") self.close();
return false;
}
function mouseDown() {
if (event.button == "2" || event.button == "3"){handle();}
}
function mouseUp(e) {
//if (document.layers || (document.getElementById && !document.all)){
if (e.which == "2" || e.which == "3"){ handle();}
//}
}
document.onmousedown=mouseDown;
document.onmouseup=mouseUp;
document.oncontextmenu=new Function("alert(msgpopup);return false")
</script>
回答by Asaf David
A simple and valid solution - bind to the 'copy' event and prevent it. You can also set what text will be copied (and later pasted by the user).
一个简单有效的解决方案 - 绑定到 'copy' 事件并阻止它。您还可以设置将复制的文本(以及稍后由用户粘贴)。
document.addEventListener('copy', function (e){
e.preventDefault();
e.clipboardData.setData("text/plain", "Do not copy this site's content!");
})