经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
禁止(防止)浏览器自动填充密码
来源:cnblogs  作者:LCTR  时间:2019/4/10 8:52:57  对本文有异议
  • 由于设置autocomplete属性和添加隐藏文本框,以及在初始化时阻止写入等方法都无法完全满足需求,所以只能通过js逻辑来控制。

 

  • 效果图

 

  • 主要代码  
    1.   /**
    2. *
    3. * 禁止浏览器自动填充密码
    4. *
    5. * @method disabledRememberPassword
    6. * @param {any} el 目标(可多个)
    7. *
    8. */
    9. function disabledRememberPassword(el) {
    10. var _el = typeof el === 'object' ? el : $(el);
    11. if (!_el || _el.length == 0)
    12. return;
    13. _el.each(function (index, item) {
    14. $(item).attr('ilength', 0).attr('autocomplete', 'off').attr('type', 'text').attr('readonly', 'readonly').val('').on('focus', function () {
    15. this.type != 'password' ? this.type = 'password' : 1;
    16. }).on('mouseout', function () {
    17. this.setAttribute('readonly', 'readonly');
    18. }).on('mouseover', function () {
    19. this.removeAttribute('readonly');
    20. }).on('input', function () {
    21. this.setAttribute('ilength', this.value.length > item.attributes['ilength'].value ? ++item.attributes['ilength'].value : --item.attributes['ilength'].value);
    22. });
    23. var clear = () => {
    24. !item.value ? setTimeout(check, 500) : (item.value = '', setTimeout(clear, 100));
    25. };
    26. var check = () => {
    27. item.value.length != item.attributes['ilength'].value ? (item.setAttribute('ilength', 0), item.value.length == 0 ? setTimeout(check, 500) : (layer.tips('检测到密码输入异常,已自动拦截', item, {
    28. tips: [2, '#000000'],
    29. time: 2000
    30. }), clear())) : setTimeout(check, 500);
    31. };
    32. check();
    33. });
    34. }

     

     

     说明:其中提示相关的代码使用的是layui的layer模块,可以换成自己想用的东西,或者不进行任何提示。

 

  • 使用方式
    1. 单个
      1. <body>
      2. <input id="password"/>
      3. </body>
      4. <script>
      5.   $(function(){
      6.     disabledRememberPassword('#password');
      7.     //或者
      8.     disabledRememberPassword($('#password'));
      9.   })
      10. </script>

       

       

    2. 多个
      1. <body>
      2. <input id="password_0">
      3. <input id="password_1">
      4. ......
      5. </body>
      6. <script>
      7. $(function(){
      8. disabledRememberPassword('#password_0,#password_1')
      9. //或者
      10. disabledRememberPassword($('#password_0,#password_1'))
      11. })
      12. </script>

       

    3. 也可以直接写在body的onload中
      1. <body onload="disabledRememberPassword('#password')">
      2. <input id="password" />
      3. </body>

       

  • 总结

      其实比较完善的解决方式是

      1. 在登录页以及其他会使浏览器提示是否记住密码的页面,放置说明和提示,告知用户这样操做会存在风险
      2. 在需要输入密码的地方使用这个js方法进行防范

  

  

  如果这些有帮助到你,记得点个赞哟。有什么疑问的话,可以在评论区留言。

 

原文链接:http://www.cnblogs.com/lctr/p/10676271.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号