Detecting Browser Autofill

前端 未结 29 1451
天涯浪人
天涯浪人 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条回答
  •  -上瘾入骨i
    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"))
    

提交回复
热议问题