Detecting Browser Autofill

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

    I had the same problem and I've written this solution.

    It starts polling on every input field when the page is loading (I've set 10 seconds but you can tune this value).
    After 10 seconds it stop polling on every input field and it starts polling only on the focused input (if one). It stops when you blur the input and again starts if you focus one.

    In this way you poll only when really needed and only on a valid input.

    // This part of code will detect autofill when the page is loading (username and password inputs for example)
    var loading = setInterval(function() {
        $("input").each(function() {
            if ($(this).val() !== $(this).attr("value")) {
                $(this).trigger("change");
            }
        });
    }, 100);
    // After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
    setTimeout(function() {
        clearInterval(loading);
    }, 10000);
    // Now we just listen on the focused inputs (because user can select from the autofill dropdown only when the input has focus)
    var focused;
    $(document)
    .on("focus", "input", function() {
        var $this = $(this);
        focused = setInterval(function() {
            if ($this.val() !== $this.attr("value")) {
                $this.trigger("change");
            }
        }, 100);
    })
    .on("blur", "input", function() {
        clearInterval(focused);
    });
    

    It does not work quite well when you have multiple values inserted automatically, but it could be tweaked looking for every input on the current form.

    Something like:

    // This part of code will detect autofill when the page is loading (username and password inputs for example)
    var loading = setInterval(function() {
        $("input").each(function() {
            if ($(this).val() !== $(this).attr("value")) {
                $(this).trigger("change");
            }
        });
    }, 100);
    // After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
    setTimeout(function() {
        clearInterval(loading);
    }, 10000);
    // Now we just listen on inputs of the focused form
    var focused;
    $(document)
    .on("focus", "input", function() {
        var $inputs = $(this).parents("form").find("input");
        focused = setInterval(function() {
            $inputs.each(function() {
                if ($(this).val() !== $(this).attr("value")) {
                    $(this).trigger("change");
                }
            });
        }, 100);
    })
    .on("blur", "input", function() {
        clearInterval(focused);
    });
    
    0 讨论(0)
  • 2020-11-22 06:44

    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
    }
    
    0 讨论(0)
  • 2020-11-22 06:45

    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
    0 讨论(0)
  • 2020-11-22 06:45

    My solution:

    Listen to change events as you would normally, and on the DOM content load, do this:

    setTimeout(function() {
        $('input').each(function() {
            var elem = $(this);
            if (elem.val()) elem.change();
        })
    }, 250);
    

    This will fire the change events for all the fields that aren't empty before the user had a chance to edit them.

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

    I succeeded on chrome with :

        setTimeout(
           function(){
              $("#input_password").focus();
              $("#input_username").focus();
              console.log($("#input_username").val());
              console.log($("#input_password").val());
           }
        ,500);
    
    0 讨论(0)
  • 2020-11-22 06:46

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

    $('#email').on('blur input', function() {
        ....
    });
    
    0 讨论(0)
提交回复
热议问题