h5password自动记录取消

sunshq sunshq     2022-12-20     269

关键词:

最近完成一个项目时需要取消谷歌浏览器的密码自动填充功能,为了用户方便,大多浏览器都有保存某个网站的密码并在后面再打开这个网站且需要输入密码的时候自动填充。这个功能是方便,但是我们有时候不需要使用这个功能,怎么样通过前端代码的写法来实现禁用呢,这就是本文的重要内容。

我在项目里遇到的就是这样一个情景,我打开项目网站的登录页面输入用户名密码登录成功,此时谷歌浏览器问是否保存密码,点击保存,然后在我项目里面有修改密码页面,其中包括了旧密码、新密码、重复新密码三个type为password的input标签,当我选中的时候它会有一个下拉选项把保存的密码填充进去(如下图),这显然不是我所需要的,将我用的方式记录下来,有用的话还请点个赞。

技术分享图片

须知

登录页面可以是自动填充的,不需禁用,以下我只是用登录页面来举例,具体需要禁用的是类似修改密码页面。

  • input标签

首先以下的都是在chorme浏览器上的运行结果,chorme在遇到type=’password’的时候才会有自动填充密码,一般只有单独的type=’text’的input标签是没有填充的。所以一旦我们包含type=’password’的input标签chorme就会把你在当前网站(注意不是网址,是你的整个项目)下保存的密码填充进去。

  • autocomplete属性

网上大多数人都说只需要将input标签的autocomplete属性设置为off就行了,事实上呢在一些浏览器上是可以的,但是有些浏览器就不行了,比如chorme(其它浏览器没有试过),chorme老版本的浏览器。

还有人说将autocomplete属性设置为new-password,这时候发现是没有自动填充了,但是点击输入时会有下拉选项选择填充密码(如下图),这不还是没取消吗。

技术分享图片

还有说在初始化页面的时候设置type=’text’,在获取到焦点的时候再修改为type=’password’,效果和上面一样。

还有就是添加两个隐藏的input标签(要对应写name属性)来欺骗浏览器,且不说方法很低级(这里我就不贴出来了),最后效果和上面一样。


我的方法

由于自动填充密码是浏览器后面完成的,所以去修改源码不太现实,这和自己造轮子没什么区别。我们只需要让浏览器找不到填充的位置就可以了。具体方式就是先告诉浏览器我的type=passowrd的input标签是一个新创建的密码,也就是设置autocomplete=”new-password”,然后不给这个input标签设置id和name属性,这样就无法填充了,核心代码如下:

 <div class="input-group inputStyle">
    <span class="glyphicon glyphicon-lock dropdown-toggle input-group-addon" style="top: 0px"></span>
    <input class="form-control inputTextStyle" type="password" autocomplete="new-password" placeholder="请输入密码">
</div>
  • 1
  • 2
  • 3
  • 4

没有id和name属性我们要获取这个标签的值的话可以使用classs属性或者通过父元素来获取:

# 通过父元素获取密码值
pwd = $(‘.inputStyle input[type=password]‘).val();
  • 1
  • 2

登录页面的完整代码和效果图如下:

技术分享图片

<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>欢迎登录</title>
    <!-- Bootstrap CSS -->
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
        @media (max-width: 768px) 
            body
                margin: 0;
                padding: 0;
                background: #ddd;
            
        
        @media (min-width:768px) 
            body
                margin: 0;
                padding: 0;
                background-size:1920px 949px;
                background-image: url(‘/static/img/桌面背景.jpg‘);
                width: 1920px;
                height: 949px;
            
            #myLoginView
                width: 665px;
                padding: 0 0px 20px 0px;
                position: absolute;
                z-index: 1;
                top: 40%;
                left: 50%;
                transform: translate(-50%, -50%);
            
        
        .code 
            font-family:Arial;
            font-style:italic;
            color:#ffffff;
            font-size:14px;
            border:0;
            cursor:pointer;
            width:120px;
            height:40px;
            line-height: 40px;
            text-align:center;
            vertical-align:center;
            display: inline-block;
            border-radius: 5px;
        
        a 
            text-decoration:none;
            font-size:12px;
            color:#288bc4;
        
        a:hover 
           text-decoration:underline;
        
        .topTitle

            position: relative;
            background: #288bc4;
            height: 70px;
            line-height: 70px;
            width: 100%;
            color: #ffffff;
            font-size: 25px;
        /*  text-align: center;*/
        
        .inputStyle
            margin-top: 10px;
            border-radius: 6px;
            margin: 20px 20px 0 20px;
        
        .inputTextStyle
        
            width: 400px;
            font-size: 14px;
            height: 50px;
            padding-left: 10px;
        
        .inputCodeStyle
            width: 280px;
            font-size: 14px;
            height: 50px;
            border-radius: 5px;
            padding-left: 10px;
        
        .loginBtn
            position: relative;
            color: #ffffff;
            background: #288bc4;;
            height: 50px;
            line-height: 50px;
            text-align: center;
            width: 93%;
            border-radius: 5px;
            font-size: 20px;
            margin: 10px 20px 0 20px;
        
        input
            outline:none;
        
        .background
           width: 100%;
           height: 100%;
           background: rgba(200,200,200,0.7);
      /*     background-image: url(‘/static/img/桌面背景.jpg‘) no-repeat;*/
           background-size:100% 100%;
        
    </style>

    </head>
    <body>
        <div class="bg bg-blur"></div> 
        <div class="background">
            <div class="col-md-4,col-sm-4,col-lg-4  center-block " id="myLoginView">
                <div style="background: #ffffff;width: 520px;padding-bottom: 20px;margin:40px 72.5px 0 72.5px;">
                    <div class="topTitle">
                        <div  style="display: inline-block;align-self: center;margin-left: 40%;">用户登录</div>
                    </div>
                    <div class="input-group inputStyle"><span class="glyphicon glyphicon-user dropdown-toggle input-group-addon" style="top: 0px"></span>
                        <input id="userId" class="inputTextStyle form-control" type="text" name="userId" placeholder="请输入用户名">
                    </div>
                    <div class="input-group inputStyle">
                        <span class="glyphicon glyphicon-lock dropdown-toggle input-group-addon" style="top: 0px"></span>
                        <input class="form-control inputTextStyle" type="password" autocomplete="new-password" placeholder="请输入密码">
                    </div>
                    <div class="input-group" style="display: flex;justify-content: space-between; align-items: center; margin: 20px 20px 0 20px;">
                        <input class="inputCodeStyle" type="text" name="controlId" placeholder="请输验证码" type="text" id="inputCode">
                        <div class="code" id="checkCode" onclick="createCode();">
                            <canvas id="verifyCanvas" width="120" height="40" style="cursor: pointer;border-radius: 5px;">您的浏览器版本不支持canvas</canvas>
                        </div>
                    </div>
                    <input class="loginBtn" id="Button1" onclick="validateCode();" type="button" value="立即登录"/>
                </div>
            </div>
        </div>
        <!-- jQuery -->
        <script src="/static/js/jquery-3.2.1.min.js"></script>
        <script>
            $.ajaxSetup(
                data: csrfmiddlewaretoken:  csrf_token ‘ ,
            );
        </script>
        <!-- jQuery -->
        <script src="/static/js/gVerify.js"></script>
        <!-- Bootstrap JavaScript -->
        <script src="/static/js/bootstrap.min.js"></script>
        <!-- code.js -->
        <script src="/static/js/loginCheck.js"></script>
    </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154

注意,上诉代码你拿去运行是不会自动填充的,你得在你的项目中登录并在浏览器弹出保存用户密码的时候保存密码再打开你的登录页面才会自动填充。其次当你的页面中只包含type=password的input标签的时候上诉方法就有问题了,比如在修改密码页面,首先我们不需要它自动填充密码,而且修改密码页面一般只有旧密码、新密码、重复新密码三个需要type=password的input标签,这时候没有type=text的input标签,此时用上面的方法就有问题了,解决办法是加一个隐藏type=text的input标签,不加的效果如下:

技术分享图片

下面是修改页面的body部分代码:

<div class="col-lg-10 col-lg-offset-1 col-md-offset-1 centerDiv col-md-10 col-sm-12  col-xs-12">
    <div class="form-horizontal">
        <div class="form-group">
            <label class="col-sm-2 control-label">输入旧密码</label>
            <div class="col-sm-10" id="oldPwd">
                <!-- 这里添加了一个隐藏的type=text的input标签 -->
                <input type="text" style="display: none;">
                <input type="password" class="form-control"  placeholder=""  autocomplete="new-password">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">输入新密码</label>
            <div class="col-sm-10" id="pwd">
                <input type="password" class="form-control"  placeholder="" autocomplete="new-password">
            </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">确定新密码</label>
          <div class="col-sm-10" id="pwdMore">
              <input placeholder="" class="form-control" type="password" autocomplete="new-password">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-default btnStyle" id="eidtPwd">确定</button>
            <button type="button" class="btn btn-default btnStyle" id="reset">重置</button>
             <button type="button" class="btn btn-default btnStyle" id="goback">返回</button>
          </div>
        </div>
  </div>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

不加的效果就是前面第一张图片。

windows的取消ftp自动保存的用户和密码

...码。这样十分不便,其实windows是默认在Internet选项中自动记录了表单的用户名和密码。取消设置:IE-> Internet选项 -> 内容-> 自动完成-> 设置-> 删除自动完成的历史记录-> 表单数据和密码 ->确定。 查看详情

ant-designinput组件取消自动显示输入历史(代码片段)

Form默认开启aotuComplete功能,当submit时会记录输入历史,取消自动补充功能,只需在Form上加autoComplete=“off”即可关闭自动提示输入历史<FormautoComplete="off"><Form.Item>getFieldDecorator(‘value‘)(<Input/>)</Form.I 查看详情

谷歌浏览器自动删除历史记录

参考技术A1.谷歌浏览器怎么每次退出自动清除历史记录1、打开浏览器,找到右上角自定义及控制按钮。2、找到设置点击打开。3、打开设置之后,滚动鼠标,找到显示更多设置,点击。4、找到“隐私设置”,点击“内容设置”... 查看详情

springboot报错记录

...新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决解决方案1:@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)//@SpringBootApplication@MapperScan("com.example.*")//扫描:该包下相应的class,主要是MyBa 查看详情

chrome退出清除历史记录

参考技术A1.谷歌浏览器怎么每次退出自动清除历史记录1、打开浏览器,找到右上角自定义及控制按钮。2、找到设置点击打开。3、打开设置之后,滚动鼠标,找到显示更多设置,点击。4、找到“隐私设置”,点击“内容设置”... 查看详情

视图高度降低

...。我正在显示视图中的一个tbaleview和工具栏。我取消选中自动布局,也取消选中底部自动调整掩码。为什么会这样?当我在iphone 查看详情

取消记录更改

】取消记录更改【英文标题】:CancellingRecordChanges【发布时间】:2012-04-1320:51:23【问题描述】:我正在使用dbedit组件编辑记录,我有一个取消按钮,但我不确定如何制作它,因此使用dbedit组件所做的所有更改都将被还原。我正在... 查看详情

linux如何取消开机自动挂载

因为我的是sata硬盘,我的Fedora8每次开机都会自动挂载Windows分区到Media下,个人用起来很不习惯,想把Windows分区挂在MNT里。可是当我辛苦地把它自动挂载的分区删除然后挂在MNT里,只要一重启,我手动挂载的分区就没了,而Media... 查看详情

linux下history那个命令所记录的命令是不是会自动清除

linux下history那个命令所记录的命令是否会自动清除?我的history命令昨天还1000条今天就100条了奇怪?NoEveR527-江湖新秀四级你好,现在问题是:我原来的历史命令绝对1000多条,现在就100条了,很奇怪!他自动覆盖会取消原来的吗... 查看详情

PostgreSQL 临时表是不是已经取消记录?

】PostgreSQL临时表是不是已经取消记录?【英文标题】:ArePostgreSQLtemporarytablesalreadyunlogged?PostgreSQL临时表是否已经取消记录?【发布时间】:2020-04-0210:41:43【问题描述】:我正在处理一个数据库操作,该操作会产生大量插入(~15,0... 查看详情

Pandas:取消组合并融化空格缩进的记录

】Pandas:取消组合并融化空格缩进的记录【英文标题】:Pandas:ungroupandmeltspace-indentedrecords【发布时间】:2020-01-2712:40:56【问题描述】:我是python&pandas的新手,能否请您告知我是否可以取消分组和取消透视此类数据框?源数据... 查看详情

如何取消word中的自动更新时间

参考技术Aword中取消自动更新时间方法如下:1、点击插入-》日期和时间;2、取消自动更新;3、确定,完成。 查看详情

是否可以使用 Relay Modern 命令式突变 API 取消链接记录?

】是否可以使用RelayModern命令式突变API取消链接记录?【英文标题】:IsitpossibletounlinkalinkedrecordusingtheRelayModernimperativemutationAPI?【发布时间】:2018-04-0206:12:36【问题描述】:我想编写一个突变updater,将链接的记录与另一条记录取... 查看详情

在应用内购买中取消自动续订订阅

】在应用内购买中取消自动续订订阅【英文标题】:CancelAuto-RenewableSubscriptioninIn-apppurchase【发布时间】:2015-10-0723:25:58【问题描述】:我在我的应用程序中使用自动更新订阅,当我测试它时。我想在Sandboxenvironment中取消自动续订... 查看详情

crm使用soap取消用户訪问记录权限

//取消訪问权限functiondemo(){   //操作记录的id   vartargetId="A8A46444-BA10-E411-8A04-00155D002F02";   //操作记录的实体名称   vartargetType="new_ 查看详情

URLSessionUploadTask 立即自动取消

】URLSessionUploadTask立即自动取消【英文标题】:URLSessionUploadTaskgettingautomaticallycancelledinstantly【发布时间】:2016-07-1523:32:15【问题描述】:我遇到了一个奇怪的问题,新创建的URLSessionUploadTask立即被取消。我不确定这是否是当前Xcod... 查看详情

如何获得自动续订订阅的取消日期?

】如何获得自动续订订阅的取消日期?【英文标题】:HowtogetcancellationdateofAuto-Renewablesubscription?【发布时间】:2012-09-2408:27:34【问题描述】:我正在开发通过IAP销售“自动续订订阅”的iOS应用。这个应用程序必须知道“取消订阅... 查看详情

Android CoroutineScope 完成后自动取消

】AndroidCoroutineScope完成后自动取消【英文标题】:AndroidCoroutineScopeAutoCancelafterItFinishes【发布时间】:2021-10-1813:51:49【问题描述】:我想知道coroutineScope工作完成后是否会自动取消。假设我在自定义类中创建了一个coroutineScope而不... 查看详情