Disable Firefox's Auto-fill

前端 未结 11 1300
天涯浪人
天涯浪人 2020-12-03 13:54

Is it possible to disable Firefox\'s auto-fill feature without disabling auto-complete?

I know I can do this:

autocomplete=\"off\"
<         


        
相关标签:
11条回答
  • 2020-12-03 14:10

    I had this problem myself. Mike's answer is close, but not perfect in my opinion: it empties elements, even if a value attribute may want to set it.

    On pageload, I do the following. It uses only a little jQuery.

    // Reset input elements to their HTML attributes (value, checked)
    $("input").each(function(){
        // match checkbox and radiobox checked state with the attribute
        if((this.getAttribute('checked')==null) == this.checked)
            this.checked = !this.checked;
        // reset value for anything else
        else this.value = this.getAttribute('value')||'';
    });
    
    // Select the option that is set by the HTML attribute (selected)
    $("select").each(function(){
        var opts = $("option",this), selected = 0;
        for(var i=0; i<opts.length; i++) 
            if(opts[i].getAttribute('selected')!==null) 
                selected = i;
    
        this.selectedIndex = selected||0;
    }); 
    


    No jQuery?

    Use document.getElementsByTagName to select the inputs and selects, then iterate over them and replace this by the iterated element.
    Select the options with .getElementsByTagName('option').

    0 讨论(0)
  • 2020-12-03 14:13

    I've had a similar issue for the password field in a form on the page.

    If there is an input tag with type = password in a form tag on the page Firefox is just automatically populating the password:

    <form>
        <input type='password' />
    </form>
    

    Here's the code I used to fix this specific issue:

    $('input[type=password]').val('')
    
    0 讨论(0)
  • 2020-12-03 14:14

    Putting two dummy fields in between the actual username field and the actual password field worked for me, e.g.:

    <input name = "Username" type="text" autocomplete="off">
    <input name = "DummyUsername" type="text" style="display:none;">
    <input name = "DummyPassword" type="password" style="display:none;">
    <input name = "Password" type="password" autocomplete="new-password">
    

    This prevented the autofill of the username, as well as the autofill of the password.

    autocomplete="new-password" is apparently not fully implemented, but can help with some browsers. I doubt that it was helpful with FireFox, but it is there for future versions in case the feature is more widely adopted.

    0 讨论(0)
  • 2020-12-03 14:15

    Firefox only honors autocomplete="off" on the form, not individual elements (unlike IE).

    Another option is to change the field names to strange things like name="_u_sern_ame" to confuse any field recognition systems if you want to disable the autofill of someones details.

    0 讨论(0)
  • 2020-12-03 14:17

    what worked for me is

    autocomplete="new-password"
    
    0 讨论(0)
  • 2020-12-03 14:20

    This actually works with a password field in FireFox:

    $(window).load(function(){
        $('#pass').val('');
    });
    
    0 讨论(0)
提交回复
热议问题