Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Browser Autofill

People also ask

How do I find my browser autofill?

Open your Chrome browser. Click on the three dots at the top right corner. Go to Settings, look for the Autofill section, and select Passwords. You will see a list of websites with usernames and passwords.

How do I know if autofill is on?

To enable or disable autofill for addresses in Chrome, click the toggle switch next to Save and fill addresses. You can try out your autofill settings using the example test form below. You may also create autofill entries manually, and change or delete them.

What is browser autofill?

1. In web browsers, autofill is a feature that automatically populates form fields with previously-entered information, such as passwords, addresses, and credit card data. For this sensitive information to be stored, the autofill feature must be enabled and have appropriate permissions.


The problem is autofill is handled differently by different browsers. Some dispatch the change event, some don't. So it is almost impossible to hook onto an event which is triggered when browser autocompletes an input field.

  • Change event trigger for different browsers:

    • For username/password fields:

      1. Firefox 4, IE 7, and IE 8 don't dispatch the change event.
      2. Safari 5 and Chrome 9 do dispatch the change event.
    • For other form fields:

      1. IE 7 and IE 8 don't dispatch the change event.
      2. Firefox 4 does dispatch the change change event when users select a value from a list of suggestions and tab out of the field.
      3. Chrome 9 does not dispatch the change event.
      4. Safari 5 does dispatch the change event.

You best options are to either disable autocomplete for a form using autocomplete="off" in your form or poll at regular interval to see if its filled.

For your question on whether it is filled on or before document.ready again it varies from browser to browser and even version to version. For username/password fields only when you select a username password field is filled. So altogether you would have a very messy code if you try to attach to any event.

You can have a good read on this HERE


Solution for WebKit browsers

From the MDN docs for the :-webkit-autofill CSS pseudo-class:

The :-webkit-autofill CSS pseudo-class matches when an element has its value autofilled by the browser

We can define a void transition css rule on the desired <input> element once it is :-webkit-autofilled. JS will then be able to hook onto the animationstart event.

Credits to the Klarna UI team. See their nice implementation here:

  • CSS rules
  • JS hook

This works for me in the latest Firefox, Chrome, and Edge:

$('#email').on('blur input', function() {
    ....
});

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


For google chrome autocomplete, this worked for me:

if ($("#textbox").is(":-webkit-autofill")) 
{    
    // the value in the input field of the form was filled in with google chrome autocomplete
}