Detecting Browser Autofill

前端 未结 29 1440
天涯浪人
天涯浪人 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:36

    Unfortunately the only reliable way i have found to check this cross browser is to poll the input. To make it responsive also listen to events. Chrome has started hiding auto fill values from javascript which needs a hack.

    • Poll every half to third of a second ( Does not need to be instant in most cases )
    • Trigger the change event using JQuery then do your logic in a function listening to the change event.
    • Add a fix for Chrome hidden autofill password values.

      $(document).ready(function () {
          $('#inputID').change(YOURFUNCTIONNAME);
          $('#inputID').keypress(YOURFUNCTIONNAME);
          $('#inputID').keyup(YOURFUNCTIONNAME);
          $('#inputID').blur(YOURFUNCTIONNAME);
          $('#inputID').focusin(YOURFUNCTIONNAME);
          $('#inputID').focusout(YOURFUNCTIONNAME);
          $('#inputID').on('input', YOURFUNCTIONNAME);
          $('#inputID').on('textInput', YOURFUNCTIONNAME);
          $('#inputID').on('reset', YOURFUNCTIONNAME);
      
          window.setInterval(function() {
              var hasValue = $("#inputID").val().length > 0;//Normal
              if(!hasValue){
                  hasValue = $("#inputID:-webkit-autofill").length > 0;//Chrome
              }
      
              if (hasValue) {
                  $('#inputID').trigger('change');
              }
          }, 333);
      });
      
    0 讨论(0)
  • 2020-11-22 06:37

    In Chrome and Edge (2020) checking for :-webkit-autofill will tell you that the inputs have been filled. However, until the user interacts with the page in some way, your JavaScript cannot get the values in the inputs.

    Using $('x').focus() and $('x').blur() or triggering a mouse event in code don't help.

    See https://stackoverflow.com/a/35783761/32429

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

    If you only want to detect whether auto-fill has been used or not, rather than detecting exactly when and to which field auto-fill has been used, you can simply add a hidden element that will be auto-filled and then check whether this contains any value. I understand that this may not be what many people are interested in. Set the input field with a negative tabIndex and with absolute coordinates well off the screen. It's important that the input is part of the same form as the rest of the input. You must use a name that will be picked up by Auto-fill (ex. "secondname").

    var autofilldetect = document.createElement('input');
    autofilldetect.style.position = 'absolute';
    autofilldetect.style.top = '-100em';
    autofilldetect.style.left = '-100em';
    autofilldetect.type = 'text';
    autofilldetect.name = 'secondname';
    autofilldetect.tabIndex = '-1';
    

    Append this input to the form and check its value on form submit.

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

    After research the issue is that webkit browsers does not fire change event on autocomplete. My solution was to get the autofill class that webkit adds and trigger change event by myself.

    setTimeout(function() {
     if($('input:-webkit-autofill').length > 0) {
       //do some stuff
     }
    },300)
    

    Here is a link for the issue in chromium. https://bugs.chromium.org/p/chromium/issues/detail?id=636425

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

    I also faced the same problem where label did not detect autofill and animation for moving label on filling text was overlapping and this solution worked for me.

    input:-webkit-autofill ~ label {
        top:-20px;
    } 
    
    0 讨论(0)
  • 2020-11-22 06:39

    On chrome, you can detect autofill fields by settings a special css rule for autofilled elements, and then checking with javascript if the element has that rule applied.

    Example:

    CSS

    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0 30px white inset;
    }
    

    JavaScript

      let css = $("#selector").css("box-shadow")
      if (css.match(/inset/))
        console.log("autofilled:", $("#selector"))
    
    0 讨论(0)
提交回复
热议问题