Html 输入类型=密码,不要让浏览器记住密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/468288/
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
Input type=password, don't let browser remember the password
提问by DavGarcia
I remember seeing a way to have an <input type="password" />
such that the browser will notprompt the user to save the password. But I'm drawing a blank. Is there an HTML attribute or some JavaScript trick that will do this?
我记得看到有一种方法可以让<input type="password" />
浏览器不会提示用户保存密码。但我正在画一个空白。是否有 HTML 属性或一些 JavaScript 技巧可以做到这一点?
回答by tvanfosson
Try using autocomplete="off"
. Not sure if every browser supports it, though. MSDN docs here.
尝试使用autocomplete="off"
. 不过,不确定是否每个浏览器都支持它。MSDN 文档在这里。
EDIT: Note: most browsers have dropped support for this attribute. See Is autocomplete="off" compatible with all modern browsers?
编辑:注意:大多数浏览器已经放弃了对这个属性的支持。请参阅autocomplete="off" 是否与所有现代浏览器兼容?
This is arguably something that should be left up to the user rather than the web site designer.
这可以说是应该留给用户而不是网站设计者的事情。
回答by matpie
<input type="password" autocomplete="off" />
<input type="password" autocomplete="off" />
I'd just like to add that as a user I think this is veryannoying and a hassle to overcome. I strongly recommend against using this as it will more than likely aggravate your users.
我只想补充一点,作为用户,我认为这很烦人,而且很麻烦。我强烈建议不要使用它,因为它很可能会激怒您的用户。
Passwords are already not stored in the MRU, and correctly configured public machines will not even save the username.
密码已经没有存储在 MRU 中,正确配置的公共机器甚至不会保存用户名。
回答by SarwarCSE
I solved in another way. You can try this.
我用另一种方式解决了。你可以试试这个。
<input id="passfld" type="text" autocomplete="off" />
<script type="text/javascript">
// Using jQuery
$(function(){
setTimeout(function(){
$("input#passfld").attr("type","password");
},10);
});
// or in pure javascript
window.onload=function(){
setTimeout(function(){
document.getElementById('passfld').type = 'password';
},10);
}
</script>
#another way
#其它的办法
<script type="text/javascript">
function setAutoCompleteOFF(tm){
if(typeof tm =="undefined"){tm=10;}
try{
var inputs=$(".auto-complete-off,input[autocomplete=off]");
setTimeout(function(){
inputs.each(function(){
var old_value=$(this).attr("value");
var thisobj=$(this);
setTimeout(function(){
thisobj.removeClass("auto-complete-off").addClass("auto-complete-off-processed");
thisobj.val(old_value);
},tm);
});
},tm);
}catch(e){}
}
$(function(){
setAutoCompleteOFF();
});
</script>
// you need to add attribute autocomplete="off" or you can add class .auto-complete-off into the input box and enjoy
// 您需要添加属性 autocomplete="off" 或者您可以将 class .auto-complete-off 添加到输入框中并享受
Example:
例子:
<input id="passfld" type="password" autocomplete="off" />
OR
<input id="passfld" class="auto-complete-off" type="password" />
回答by Apostolidis
I tried the following and it seems that works to any browser:
我尝试了以下操作,似乎适用于任何浏览器:
<input id="passfld" type="text" autocomplete="off" />
<script type="text/javascript">
$(function(){
var passElem = $("input#passfld");
passElem.focus(function() {
passElem.prop("type", "password");
});
});
</script>
This way is much more safer than using timeout techniques, because it guaranties that the input field will yield to password when the user focuses it.
这种方式比使用超时技术要安全得多,因为它保证当用户聚焦时输入字段将屈服于密码。
回答by Michael
As for security issues, here is what a security consultant will tell you on the whole field issue (this is from an actual independent security audit):
至于安全问题,以下是安全顾问将在整个现场问题上告诉您的内容(这是来自实际的独立安全审计):
HTML Autocomplete Enabled – Password fields in HTML forms have autocomplete enabled. Most browsers have a facility to remember user credentials entered into HTML forms.
Relative Risk: Low
Affected Systems/Devices: o https://*******/
启用 HTML 自动完成 - HTML 表单中的密码字段已启用自动完成。大多数浏览器都具有记住输入到 HTML 表单中的用户凭据的功能。
相对风险:低
受影响的系统/设备:o https:// *** ****/
I also agree this should cover anyfield that contains truly private data. I feel that it is alright to force a person to always type their credit card information, CVC code, passwords, usernames, etc whenever that site is going to access anything that should be kept secure [universally or by legal compliance requirements]. For example: purchase forms, bank/credit sites, tax sites, medical data, federal, nuclear, etc - notSites like Stack Overflow or Facebook.
我也同意这应该涵盖任何包含真正私人数据的领域。我认为,只要该站点要访问任何应该保持安全 [普遍或法律合规性要求] 的内容,强迫一个人总是输入他们的信用卡信息、CVC 代码、密码、用户名等是可以的。例如:购买表格、银行/信用网站、税务网站、医疗数据、联邦、核等 -不是Stack Overflow 或 Facebook等网站。
Other types of sites - e.g. TimeStar Online for clocking in and out of work - it's stupid, since I always use the same PC/account at work, that I can't save the credentials on that site - strangely enough I can on my Android but not on an iPad. Even shared PCs this wouldn't be too bad since clocking in/out for someone else really doesn't do anything but annoy your supervisor. (They have to go in and delete the erroneous punches - just choose not to save on public PCs).
其他类型的网站 - 例如 TimeStar Online 用于打卡和下班 - 这很愚蠢,因为我总是在工作中使用相同的 PC/帐户,我无法在该站点上保存凭据 - 奇怪的是我可以在我的 Android 上但不是在 iPad 上。即使是共享的 PC,这也不会太糟糕,因为为其他人打卡/打卡确实不会做任何事情,只会惹恼您的主管。(他们必须进入并删除错误的拳 - 只是选择不保存在公共 PC 上)。
回答by Captain Fantastic
Here's the best answer, and the easiest! Put an extra password field in front of your input
field and set the display:none
, so that when the browser fills it in, it does it in an input
that you don't care about.
这是最好的答案,也是最简单的!在您的input
字段前面放置一个额外的密码字段并设置display:none
,以便当浏览器填写它时,它会以input
您不关心的方式执行。
Change this:
改变这个:
<input type="password" name="password" size="25" class="input" id="password" value="">
to this:
对此:
<input type="password" style="display:none;">
<input type="password" name="password" size="25" class="input" id="password" value="">
回答by Bhaumik Thakkar
<input type="password" placeholder="Enter New Password" autocomplete="new-password">
Here you go.
干得好。
回答by Ferie
Read also this answerwhere he is using this easy solution that works everywhere (see also the fix for Safari mobile):
另请阅读此答案,其中他使用了这种适用于任何地方的简单解决方案(另请参阅 Safari 移动版的修复程序):
<input type="password" readonly onfocus="this.removeAttribute('readonly');"/>
回答by Chandrika Prajapati
you can also use it like following
你也可以像下面这样使用它
$('#Password').attr("autocomplete", "off");
setTimeout('$("#Password").val("");', 2000);
回答by Fernando
You can use JQuery, select the item by id:
您可以使用 JQuery,通过 id 选择项目:
$("input#Password").attr("autocomplete","off");
Or select the item by type:
或按类型选择项目:
$("input[type='password']").attr("autocomplete","off");
Or also:
或者也:
You can use pure Javascript:
您可以使用纯 Javascript:
document.getElementById('Password').autocomplete = 'off';