如何禁用剪切,复制和粘贴操作的密码框跨所有浏览器,即chrome和Internet Explorer

How to disable cut , copy and paste operation for password box across all browsers i.e chrome and Internet Explorer

本文关键字:浏览器 Explorer Internet chrome 何禁用 复制 操作 密码      更新时间:2023-09-26
<html>
<head><title>Practice</title>
<script language="javascript" type="text/javascript">  
function disableCopy()  
{  
alert("You cannot perform Copy");  
return false;  
}  
function disablePaste()  
{  
alert("You cannot perform Paste");  
return false;  
}  
function disableCut()  
{  
alert("You cannot perform Cut");  
return false;  
}  

function disableContextMenu()  
{  
alert("You cannot perform right click via mouse as well as keyboard");  
return false;  
}  
</script>
</head>
<body>
Password:<input type="password" oncopy="return disableCopy();" onpaste="return disablePaste();" oncut="return disableCut();" oncontextmenu="return disableContextMenu();"/>
</body>
</html>

如何禁用密码框的剪切,复制和粘贴操作与警告消息,这样在每一个操作,将在密码框上执行它显示一个警报提示,以便用户将被通知他不能做密码的剪切,复制和粘贴操作。我已经讲过其他使用onCopy, onCut, onPaste事件的例子。我已经为这个事件编写了函数,但是这些函数并没有提示我警报消息,当它在所有三个浏览器上运行时,请为这种情况提供帮助,这样它就可以在所有三个浏览器上运行,当onCopy, onCut,onPaste事件发生时,它会提示我一个警报。我所需要的是当我从密码字段复制或剪切密码时,我需要一个警报,说明密码不能复制作为消息。

提前感谢朋友的帮助

是否允许用户执行这些操作可能取决于应用程序的要求。要在vanilla js中实现这一点,您可以参考以下示例。

 document.getElementById('password').addEventListener('copy', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('paste', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('cut', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('drag', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('drop', function(e) {
   e.preventDefault();
 });
Password:
<input type="password" id="password" />

一种方法是使用全局处理程序,通过检查其id

可以处理多个输入。
window.addEventListener('copy', function(e) {
    if (e.target.id == "password-new") {
       e.preventDefault();
       alert("not allowed");
    }
 });
window.addEventListener('paste', function(e) {
    if (e.target.id == "password-again") {
       e.preventDefault();
       alert("not allowed");
    }
 });
etc...

如果你想在旧的IE中捕获这个,改变上面的这部分

    var target = e.target || event.srcElement;
    if (target.id == "password-new") {
       e.preventDefault();
       alert("not allowed");
    }
相关文章: