Detecting Browser Autofill

前端 未结 29 1809
天涯浪人
天涯浪人 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: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')

提交回复
热议问题