Detecting Browser Autofill

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

提交回复
热议问题