Detecting Browser Autofill

前端 未结 29 1441
天涯浪人
天涯浪人 2020-11-22 06:15

How do you tell if a browser has auto filled a text-box? Especially with username & password boxes that autofill around page load.

My first question is when does

相关标签:
29条回答
  • 2020-11-22 06:46

    From my personal experience, the below code works well with firefox IE and safari, but isn't working very well at picking autocomplete in chrome.

    function check(){
    clearTimeout(timeObj);
     timeObj = setTimeout(function(){
       if($('#email').val()){
        //do something
       }
     },1500);
    }
    
    $('#email').bind('focus change blur',function(){
     check();
    });
    

    Below code works better, because it will trigger each time when user clicks on the input field and from there you can check either the input field is empty or not.

    $('#email').bind('click', function(){
     check();
    });
    
    0 讨论(0)
  • 2020-11-22 06:46

    try in CSS

    input:-webkit-autofill { border-color: #9B9FC4 !important; }

    0 讨论(0)
  • 2020-11-22 06:47

    Just in case someone is looking for a solution (just as I was today), to listen to a browser autofill change, here's a custom jquery method that I've built, just to simplify the proccess when adding a change listener to an input:

        $.fn.allchange = function (callback) {
            var me = this;
            var last = "";
            var infunc = function () {
                var text = $(me).val();
                if (text != last) {
                    last = text;
                    callback();
                }
                setTimeout(infunc, 100);
            }
            setTimeout(infunc, 100);
        };
    

    You can call it like this:

    $("#myInput").allchange(function () {
        alert("change!");
    });
    
    0 讨论(0)
  • 2020-11-22 06:47

    There does appear to be a solution to this that does not rely on polling (at least for Chrome). It is almost as hackish, but I do think is marginally better than global polling.

    Consider the following scenario:

    1. User starts to fill out field1

    2. User selects an autocomplete suggestion which autofills field2 and field3

    Solution: Register an onblur on all fields that checks for the presence of auto-filled fields via the following jQuery snippet $(':-webkit-autofill')

    This won't be immediate since it will be delayed until the user blurs field1 but it doesn't rely on global polling so IMO, it is a better solution.

    That said, since hitting the enter key can submit a form, you may also need a corresponding handler for onkeypress.

    Alternately, you can use global polling to check $(':-webkit-autofill')

    0 讨论(0)
  • 2020-11-22 06:48

    I was reading a lot about this issue and wanted to provide a very quick workaround that helped me.

    let style = window.getComputedStyle(document.getElementById('email'))
      if (style && style.backgroundColor !== inputBackgroundNormalState) {
        this.inputAutofilledByBrowser = true
      }
    

    where inputBackgroundNormalState for my template is 'rgb(255, 255, 255)'.

    So basically when browsers apply autocomplete they tend to indicate that the input is autofilled by applying a different (annoying) yellow color on the input.

    Edit : this works for every browser

    0 讨论(0)
  • 2020-11-22 06:49

    I know this is an old thread but I can imagine many comes to find a solution to this here.

    To do this, you can check if the input(s) has value(s) with:

    $(function() {
        setTimeout(function() {
            if ($("#inputID").val().length > 0) {
                // YOUR CODE
            }
        }, 100);
    });
    

    I use this myself to check for values in my login form when it's loaded to enable the submit button. The code is made for jQuery but is easy to change if needed.

    0 讨论(0)
提交回复
热议问题