Preventing a browser from storing passwords is generally a user preference, and as such, it’s controlled by browser settings rather than by HTML or JavaScript. However, you can use the autocomplete attribute to suggest to the browser not to save passwords for a specific input field.
To do this we write
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" autocomplete="off">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="new-password">
<input type="submit" value="Submit">
</form>
to create a form.
The autocomplete="off"
attribute on the username field suggests to the browser that it should not remember previously entered values for this field.
The autocomplete="new-password"
attribute on the password field suggests to the browser that it should not suggest previously entered passwords for this field, and it should prompt the user to enter a new password if available.
However, it’s important to note that browsers may choose to ignore these suggestions for security reasons, especially in the case of password fields. Users also have control over their browser’s settings, and they may choose to ignore these suggestions or enable password saving globally.
Therefore, while you can use these attributes to suggest to the browser not to save passwords, it’s ultimately up to the browser and user whether passwords are saved or not. Additionally, JavaScript cannot directly control browser settings related to password saving.
One reply on “How to prevent a browser from storing passwords with HTML and JavaScript?”
this doesn’t work on firefox, it still keeps asking. It is really annoying because there are some use cases where this just doesn’t make sense like a user management screen where you enter someone else’s default password, or where you need to log into something else and it needs a password but its not the users password.
I have been trying to fix this for ages and have finally found a way, not sure if it works for all browsers, but the solution is simply not use a password field at all just set the style to -webkit-text-security: disc; on a normal input field (you may want to stop copy as well) (also you could make a font that is all * and use that)