源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

解决浏览器会自动填充密码的问题

  • 时间:2020-08-02 07:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:解决浏览器会自动填充密码的问题
解决办法是在form上或input上添加autoComplete="off"这个属性。 form表单的属性如下所示: [img]http://files.jb51.net/file_images/article/201704/2017428102639166.png?201732810276[/img] 但是这个解决方案在谷歌和火狐上均有bug,下面来一个一个解决。 [b]1.'autocomplete="off"'在Chrome中不起作用解决方案[/b] 网站项目中,有登录和注册的弹框,在除chrome的浏览器中一切都ok,一旦在谷歌浏览器中,问题来了: 首先从登录弹框中登陆成功,chrome会弹出是否保存密码的提示框,点击保存密码按钮, [img]http://files.jb51.net/file_images/article/201704/2017428101655274.jpg?2017328102145[/img] 然后接着退出账户, 这时打开注册弹框,你会发现注册弹框中用户名和密码也被默认填写进去了(登录弹框中默认填写进去符合逻辑), [img]http://files.jb51.net/file_images/article/201704/2017428101609633.png?2017328101849[/img] [img]http://files.jb51.net/file_images/article/201704/2017428101620533.png?2017328101935[/img] 这现象就诡异了,开始各种查,cookie,本地缓存,等等,都解决不了这问题; 查阅后,很多没有这个的解决方案。 [b]1  通常我们会在form表单上加入autocomplete="off" 或者 在输入框中加入autocomplete="off"[/b]
<form method="post" action="" name="login" autocomplete="off"> 
</form> 
//或者 
<input id="name" type="text" name="name" maxlength="20" autocomplete="off"> 
[b]2  但是有一种情况例外,就是表单中有input[type="password"],点击保存密码后,在Chrome浏览器则自动填充了用户名和密码的输入框;为了统一样式,我们需要就对Chrome的问题经行单独处理。[/b] [b]总结了4种解决方案,如下:[/b] [b]1 修改disabled属性[/b]
if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1){ 
   var inputers = document.getElementsByTagName("input"); 
   for(var i=0;i<inputers.length;i++){ 
    if((inputers[i].type !== "submit") && (inputers[i].type !== "password")){ 
     inputers[i].disabled= true; 
    } 
   } 
   setTimeout(function(){ 
    for(var i=0;i<inputers.length;i++){ 
     if(inputers[i].type !== "submit"){ 
      inputers[i].disabled= false; 
     } 
    } 
   },100) 
  } 
[b]2 去除输入框的name和id属性[/b]
if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1){ 
   var inputers = document.getElementsByTagName("input"); 
   for(var i=0;i<inputers.length;i++){ 
    if((inputers[i].type !== "submit") && (inputers[i].type !== "password")){ 
     var input = inputers[i]; 
     var inputName = inputers[i].name; 
     var inputid = inputers[i].id; 
     inputers[i].removeAttribute("name"); 
     inputers[i].removeAttribute("id"); 
     setTimeout(function(){ 
      input.setAttribute("name",inputName); 
      input.setAttribute("id",inputid); 
     },1) 
    } 
   } 
  } 
[b]3.可以在不需要默认填写的input框中设置 autocomplete="new-password"[/b] 网上咱没有找到对其详细解释,但是发现163邮箱的登录注册是这么用的, [img]http://files.jb51.net/file_images/article/201704/2017428101637069.png?2017328102021[/img] 所以就借鉴借鉴咯,测试之后也是可以解决问题的,也是最简单的解决办法,网易给您点个赞! [b]4 修改readonly属性[/b] [code]<input type="password" readonly onfocus="this.removeAttribute('readonly');"/>[/code]  但Firefox中有个Bug。首次提交后,FF会提示是否记住某某网站的密码,点击“记住”后 input[type=text]设置autocomplete="off"将不起作用。 [img]http://files.jb51.net/file_images/article/201704/2017428101644192.png?2017328102056[/img] [b]有两种情况:[/b] 1,form中没有input[type=password],autocomplete="off"将起作用 2,去掉form,设置input[type=text]的autocomplete也起作用(测试不好用) 3.Firefox则需要使用另一个扩展属性disableautocomplete  (测试也不行) [code]<input type="text"  disableautocomplete autocomplete="off"  id="number"/>[/code] 火狐现在也没有解决的办法,,谁有麻烦告知一下哈。。。。。 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部