How can I get browser to prompt to save password?

前端 未结 20 1250
面向向阳花
面向向阳花 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:30

    Not every browser (e.g. IE 6) has options to remember credentials.

    One thing you can do is to (once the user successfully logs in) store the user information via cookie and have a "Remember Me on this machine" option. That way, when the user comes again (even if he's logged off), your web application can retrieve the cookie and get the user information (user ID + Session ID) and allow him/her to carry on working.

    Hope this can be suggestive. :-)

    0 讨论(0)
  • 2020-11-22 16:32

    Simple 2020 aproach

    This will automatically enable autocomplete and save password in browsers.

    • autocomplete="on" (form)
    • autocomplete="username" (input, email/username)
    • autocomplete="current-password" (input, password)
    <form autocomplete="on">
      <input id="user-text-field" type="email" autocomplete="username"/>
      <input id="password-text-field" type="password" autocomplete="current-password"/>
    </form>
    

    Check out more at Apple's documentation: Enabling Password AutoFill on an HTML Input Element

    0 讨论(0)
  • 2020-11-22 16:34

    This solution worked for me posted by Eric on the codingforums


    The reason why it does not prompt it is because the browser needs the page to phyiscally to refresh back to the server. A little trick you can do is to perform two actions with the form. First action is onsubmit have it call your Ajax code. Also have the form target a hidden iframe.

    Code:

    <iframe src="ablankpage.htm" id="temp" name="temp" style="display:none"></iframe>
    <form target="temp" onsubmit="yourAjaxCall();">
    

    See if that causes the prompt to appear.

    Eric


    Posted on http://www.codingforums.com/showthread.php?t=123007

    0 讨论(0)
  • 2020-11-22 16:35

    You may attach the dialog to the form, so all those inputs are in a form. The other thing is make the password text field right after the username text field.

    0 讨论(0)
  • 2020-11-22 16:36

    None of the answers already make it clear you can use the HTML5 History API to prompt to save the password.

    First, you need to make sure you have at least a <form> element with a password and email or username field. Most browsers handle this automatically as long as you use the right input types (password, email or username). But to be sure, set the autocomplete values correctly for each input element.

    You can find a list of the autocomplete values here: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete

    The ones you need are: username, email and current-password

    Then you have two possibilities:

    • If you navigate away to a different URL after submitting, most browsers will prompt to save the password.
    • If you don't want to redirect to a different URL or even reload the page (e.g. a single page application). Just prevent the event defaults (using e.preventDefault) in your submit handler of the form. You can use the HTML5 history API to push something on the history to indicate you 'navigated' inside your single page application. The browser will now prompt to save the password and username.
    history.pushState({}, "Your new page title");
    

    You can also change the page's URL, but that is not required to prompt to save the password:

    history.pushState({}, "Your new page title", "new-url");
    

    Documentation: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState

    This has the additional benefit that you can prevent the browser to ask to save the password if the user entered the password incorrectly. Note that in some browsers the browser will always ask to save the credentials, even when you call .preventDefault and not use the history API.

    If you don't want to navigate away and/or modify the browser history, you can use replaceState instead (this also works).

    0 讨论(0)
  • 2020-11-22 16:36

    The truth is, you can't force the browser to ask. I'm sure the browser has it's own algorithm for guessing if you've entered a username/password, such as looking for an input of type="password" but you cannot set anything to force the browser.

    You could, as others suggest, add user information in a cookie. If you do this, you better encrypt it at the least and do not store their password. Perhaps store their username at most.

    0 讨论(0)
提交回复
热议问题