如何使用 javascript 为 Internet Explorer 和 firefox 浏览器禁用 Ctrl C/V
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15394957/
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 Ctrl C/V using javascript for both internet explorer and firefox browsers
提问by Ali Taha Ali Mahboub
I am making this javascript code in order to disable Ctlr+c and Ctlr+v, prenscreen, ALT+TAB, Ctlr+S, and PrintScreen keys.
我正在编写此 javascript 代码以禁用 Ctlr+c 和 Ctlr+v、prenscreen、ALT+TAB、Ctlr+S 和 PrintScreen 键。
<html>
<head>
<script language="javascript">
function Disable_Control_C() {
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (keystroke == 'c' || keystroke == 'v')) {
alert("let's see");
event.returnValue = false; // disable Ctrl+C
}
}
</script>
</head>
<body onkeydown="javascript:Disable_Control_C()">
Hello World!
</body>
</html>
unfortunately, code is working on IE browser, but not working on firefox. Can anyone here advice?
不幸的是,代码可以在 IE 浏览器上运行,但不能在 Firefox 上运行。这里有人可以建议吗?
回答by Matt Ball
- I don't like when browsers do this to me, and
- It's easy to work around, and
- This doesn't count as "secure" by any definition, but
- 我不喜欢浏览器对我这样做,并且
- 这很容易解决,而且
- 根据任何定义,这都不算作“安全”,但是
Use element.on(?:
copy
|cut
|paste
)
使用element.on(?:
copy
| cut
|paste
)
<body oncopy="return false" oncut="return false" onpaste="return false">
回答by Sachin
you can use it jquery for this. You just need to bind the cut
, copy
and paste
function with your element.
您可以为此使用它 jquery。您只需要将cut
,copy
和paste
function 与您的元素绑定。
And add this Jquery script:
并添加此 Jquery 脚本:
$(document).ready(function() {
$('#Selector').bind('copy paste', function(e) {
e.preventDefault();
});
});