In a situation where we have many users using the same computer with confidential information, we had to turn off password auto-completion. At first I tried your "simple and elegant" solution for hidden passwords, but a smart user showed me that everyone can use the browser developer tools to display the automatically completed password of the previous user in text form in a hidden field.
I turned off password auto-completion in a safe way as follows. This works in Firefox 29 and IE8:
Create a password field dynamically and add it to the form on the page load. Give it a placeholder attribute.
function createPasswordInput() { var dynpass = document.createElement("input"); dynpass.setAttribute("type", "password"); dynpass.setAttribute("size", 32); dynpass.setAttribute("id", "dynPwElem"); // placeholder helps prevent password autocomplete dynpass.setAttribute("placeholder", "Enter password"); // append the password element to the form, in my case an empty table cell var td = document.getElementById("dynamicTd"); td.appendChild(dynpass); var rpw = document.getElementById("dynPwElem"); rpw.setAttribute("name", "userPassword"); // Max length starts at only 1, helps prevent autocomplete. // Use property maxLength instead of setAttribute to work in IE rpw.maxLength = "1"; // set the DOM property that has uppercase L
Set the maxLength property of the password element to only 1 and attach an onfocus event to expand maxLength to what it should (32 in my case).
function expandPwLength() { var rpw = document.getElementById("dynPwElem"); // use property maxLength instead of setAttribute to work in IE if (rpw.maxLength < 32) { rpw.maxLength = 32; } } // attach focus event to expand the password field length if (rpw.addEventListener) { rpw.addEventListener("focus", expandPwLength, true); } else { rpw.attachEvent("onfocus", expandPwLength); // IE <= 8 }
Prevent form submission if the user types "Enter" in the user ID field using a keypress event listener. Instead, move the cursor to the password field.
function preventAutocomplete (charCode) { if (charCode == 13) {
In the same keystroke event listener, shorten the auto-complete property of the password item.
if (charCode == 13 || charCode == 40) // the Enter key, or the Down-arrow key document.forms["loginSubmitForm"].userPassword.autocomplete = "";
Set the property of the entire "auto-complete" form to "off" using JavaScript when the page loads, and not in the HTML tag.
document.forms["loginSubmitForm"].autocomplete="off";