Hey, I'm working on a web app that has a login dialog that works like this:
- User clicks "login"
- Login form HTML is loaded with AJAX and displayed in DIV on page
- User enters user/pass in fields and clicks submit. It's NOT a
<form>
-- user/pass are submitted via AJAX - If user/pass are okay, page reloads with user logged in.
- If user/pass are bad, page does NOT reload but error message appears in DIV and user gets to try again.
Here's the problem: the browser never offers the usual "Save this password? Yes / Never / Not Now" prompt that it does for other sites.
I tried wrapping the <div>
in <form>
tags with "autocomplete='on'" but that made no difference.
Is it possible to get the browser to offer to store the password without a major rework of my login flow?
thanks Eric
p.s. to add to my question, I'm definitely working with browers that store passwords, and I've never clicked "never for this site" ...this is a technical issue with the browser not detecting that it's a login form, not operator error :-)
I have been struggling with this myself, and I finally was able to track down the issue and what was causing it to fail.
It all stemmed from the fact that my login form was being dynamically injected into the page (using backbone.js). As soon as I embed my login form directly into my index.html file, everything worked like a charm.
I think this is because the browser has to be aware that there is an existing login form, but since mine was being dynamically injected into the page, it didn't know that a "real" login form ever existed.
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.
This work much better for me, because it's 100% ajaxed and the browser detects the login.
I found a fairly elegant solution (or hack, whatever fits) for Prototype.JS users, being one of the last holdouts using Prototype. A simple substitution of corresponding jQuery methods should do the trick.
First, make sure there's a
<form>
tag, and a submit button with a class name that can be referenced later (in this casefaux-submit
) that is nested inside an element with a style set todisplay:none
, as illustrated below:Then create a click observer for the
button
, that will "submit" the form as illustrated:Then create a listener for
submit
event, and stop it.event.stop()
will stop all submit events in the DOM unless it's wrapped inEvent.findElement
with the class of the hidden input button (as above,faux-submit
):This is tested as working in Firefox 43 and Chrome 50.
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:
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.
add a bit more information to @Michal Roharik 's answer.
if your ajax call will return a return url, you should use jquery to change the form action attribute to that url before calling form.submit
ex.