简体   繁体   English

如何在所有浏览器(例如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>

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.

相关问题 如何禁用html中密码标签的剪切、复制和粘贴操作 - How to disable cut, copy and paste operation for password tag in html 如何在所有设备(例如台式机和移动浏览器)上制作具有一定高度和100%宽度的iframe - how to make an iframe with some height and 100% width across all devices i.e desktop and mobile browsers 自定义右键单击UI上下文菜单(复制,粘贴,剪切)适用于所有浏览器的整个应用程序(所有网页)不适用于单个网页 - Custom right click UI Context Menu(copy,paste,cut) For Entire Application (All web pages) Across All Browsers not for single web page 如何在 React js 的输入框中禁用复制粘贴 - How I can disable copy paste in an input box in react js 我想禁用浏览器的后退按钮,我尝试了一个适用于 Internet Explorer 但在 chrome 中遇到问题的代码 - I want to disable browsers back button, i have tried a code which is working for internet explorer but facing problem in chrome 在IE 8和9中正常工作时,验证表达式在Internet Explorer 6中不起作用 - Validate expression is not working in Internet Explorer 6 while working fine in I.E 8 and 9 如何在java脚本或jquery中禁用浏览器url的复制粘贴? - How to disable copy paste of browsers url in java script or jquery? 禁用文本选择但允许剪切、复制和粘贴 - Disable text selection but allow cut, copy, and paste 网站可在除Internet Explorer 8之外的所有浏览器上正常运行 - Website functioning on all browsers except Internet Explorer 8 精简快捷的方法来使插件在所有主流浏览器上都能正常工作? Firefox,Chrome,[Opera,互联网浏览器,野生动物园] - lean&fast way to make an addon work for all major browsers? firefox,chrome,[opera,internet explorer,safari]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM