How can I get browser to prompt to save password?

前端 未结 20 1440
面向向阳花
面向向阳花 2020-11-22 16:20

Hey, I\'m working on a web app that has a login dialog that works like this:

  1. User clicks \"login\"
  2. Login form HTML is loaded with AJAX and displayed i
20条回答
  •  無奈伤痛
    2020-11-22 16:43

    There's an ultimate solution to force all browsers (tested: chrome 25, safari 5.1, IE10, Firefox 16) to ask for save the password using jQuery and ajax request:

    JS:

    $(document).ready(function() {
        $('form').bind('submit', $('form'), function(event) {
            var form = this;
    
            event.preventDefault();
            event.stopPropagation();
    
            if (form.submitted) {
                return;
            }
    
            form.submitted = true;
    
            $.ajax({
                url: '/login/api/jsonrpc/',
                data: {
                    username: $('input[name=username]').val(),
                    password: $('input[name=password]').val()
                },
                success: function(response) {
                    form.submitted = false;
                    form.submit(); //invoke the save password in browser
                }
            });
        });
    });
    

    HTML:

    The trick is in stopping the form to submit its own way (event.stopPropagation()), instead send your own code ($.ajax()) and in the ajax's success callback submit the form again so the browser catches it and display the request for password save. You may also add some error handler, etc.

    Hope it helped to someone.

提交回复
热议问题