parth.hirpara
parth.hirpara

Reputation: 542

How to save tweaked password to browser?

In my application, while logging in browser asked me to save password. To prevent that I used html tag autocomplete = "off" and it is not working for some browser.

For a more step security - If browser asked to save password, Password should get changed while saving the password in browse.

For that on click of login, I fetch the password from field and then changed the password with dummy password using javascript.

Code :

function onLoginClick(){
       var passStr = $("#myPassword").val();
       $("#myPassword").val('dummyPassword');
}

But while saving password, Browser is picking up the actual password not that dummy password.

Upvotes: 2

Views: 90

Answers (1)

MicronXD
MicronXD

Reputation: 2220

I believe Chrome keeps track of the last user-entered value of each input. So, simply swapping it out isn't going to overwrite that. What you may want to do is call listen for the submit event, and call the event's preventDefault function in your handler. I'm not sure exactly what each browser is looking for to trigger the "wanna save this password?" dialog, but I know that navigating away in the submit tick is one of them. Have you tried something along the lines of:

<form>
    <input name="username">
    <input type="password" name="textPass">
    <input type="hidden" name="hiddenPass">
</form>

with the js:

form.addEventListener('submit', function(e){
    if(hiddenPass.value.length === 0) {
        e.preventDefault();
        hiddenPass.value = textPass.value;
        textPass.value = '';
        window.setTimeout(function(){
            form.submit();
        });
    }
});

Or what about an invisible password field?:

<input type="password" name="password" value="" style="display: none;">
<input type="password" name="realPassword">

Upvotes: 1

Related Questions