[英]How to disable cut , copy and paste operation for password box across all browsers i.e chrome and Internet Explorer
<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>
How to disable cut , copy and paste operation for password box with alert messages , such that on every operation that will be performed on password box it shows an alert prompt so that user will be notified he can't do cut ,copy and paste operation on passwords .I had gone through other examples which uses onCopy, onCut, onPaste events.I had written functions for this events, but these functions did'nt prompt me the alert message when it was run across all three browsers please provide me help for this situation ,such that it could run across all three browsers and it prompts me an alert when the onCopy, onCut,onPaste event occurs. 如何禁用带有警报消息的密码框的剪切,复制和粘贴操作,以便在将对密码框执行的每个操作上都显示警报提示,以便通知用户他无法进行剪切,复制和粘贴操作关于密码 。我已经看过其他使用onCopy,onCut,onPaste事件的示例。我已经为此事件编写了函数,但是当这些函数在所有三个浏览器上运行时,这些函数都不会提示我警报消息 ,请为我提供帮助这种情况,使其可以在所有三个浏览器上运行,并且在发生onCopy,onCut,onPaste事件时提示我一个警报。 All I need is when I copy or cut passwords from password field , I need an alert stating that password cannot be copied as the message. 我需要做的就是从“密码”字段复制或剪切密码时 ,我需要一个警报,指出无法将密码复制为消息。
Thanks Friends in advance for the help 预先感谢朋友的帮助
Whether to allow user to perform such actions may depend upon the requirement of the application. 是否允许用户执行此类操作可能取决于应用程序的要求。 To achieve this in vanilla js , you can refer following example. 要在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" />
One way is using a global handler, where you can handle more than one input by checking its id 一种方法是使用全局处理程序,您可以通过检查其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...
If you want to catch this in older IE, change this part of the above 如果要在较旧的IE中捕获此错误,请更改上面的这一部分
var target = e.target || event.srcElement;
if (target.id == "password-new") {
e.preventDefault();
alert("not allowed");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.