如何禁用剪切,复制和粘贴操作的密码框跨所有浏览器,即chrome和Internet Explorer
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>
如何禁用密码框的剪切,复制和粘贴操作与警告消息,这样在每一个操作,将在密码框上执行它显示一个警报提示,以便用户将被通知他不能做密码的剪切,复制和粘贴操作。我已经讲过其他使用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");
}
相关文章:
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- 对于IE浏览器,我需要使用什么来禁用Internet Explorer的默认下拉样式
- Javascript:将浏览器Internet Explorer从8更新到11
- 页面内容不会在 Internet Explorer 中刷新(仅在浏览器重新打开后)
- 滑块图像在Internet Explorer中不起作用,但在其他浏览器中有效
- 浏览器支持 Internet Explorer 10 - 兼容模式
- 在 Internet Explorer 中打开多个选项卡时检测浏览器关闭事件
- Internet Explorer mimeType 始终为空,并且无法在此浏览器中获取支持的 MIME 类型
- Javascript来检测旧的Internet Explorer并提供下载较新的浏览器
- 当单击 shift + 鼠标左键单击时,Internet Explorer 浏览器问题 ,文本会自动突出显示
- Internet Explorer 作为 CasperJS 的默认浏览器
- 如何用Java调整Internet Explorer浏览器的大小
- JavaScript for Each循环适用于除Internet Explorer之外的所有Web浏览器
- 网站可在除Internet Explorer 8之外的所有浏览器上运行
- 即使Internet Explorer浏览器在设置中禁用了音频,我们也可以通过Javascript编码播放音频吗
- 在Internet Explorer中调试JavaScript时,浏览器不可交互
- 如何从其他浏览器使用网址启动Internet Explorer
- 如果浏览器是internet explorer,如何避免执行某些javascript代码
- 如何从internet explorer浏览器中使用javascript获取系统(windows)用户名
- 为什么使用window.navigator.userAgent检索浏览器Explorer 11会被识别为Mozilla?