Turn Autocomplete Off For HTML Input Tags
It’s very annoying when the autocompletion is on in your browser and you develop a website where you have to sign up a user. Of course on that form there are different text fields, username field, password field. And it is very very annoying that the autocomplete ’stuff’ fills these textboxes with your sensitive data.
From a web developers point of view it’s good practice to clear all the text boxes when the form is shown. BUT (there’s always have to be a but) of course when we want to do this we face with browser incompatibilities (again!). Let’s see IE and Safari support autocomplete=”off” attribute in html input and form tags, but not Firefox. This is a proprietary attribute, so it fails in standard’s validation.
It seems that Firefox is the black sheep in this case, because it always runs this auto complete after the whole page was rendered. So it’s useless to set a html input’s value to nothing. (value=”")
One solution to this problem is with Javascript. The idea is to load a Javascript function which sets a specified input’s value to null after the whole page was rendered. The code below does this thing. You have to set the tag’s id and it will do the rest.
var genericAutocomplete = {
off : function(srcInputID) {
var tag = document.getElementById(srcInputID)
if (tag) {
tag.value = '';
}
} //end of turnOff
} //end of genericAutocomplete
To load this after the page is rendered we call the setTimeout function like this:
window.setTimeout("genericAutocomplete.off('input_id')",50);
We have to use setTimeout to load this function after the page is fully rendered by Firefox. Loading in the window’s onload event just doesn’t work.
And as a bonus our pages now validates too.
If you enjoyed this article then help spread the word and please follow us on Twitter or subscribe to our RSS feed.
Subscribe to our RSS feedFollow us on Twitter
on Thursday 4, 2007
Hi,
I was searching around for an answer for firefox, and there’s something on the mozilla site:
http://developer.mozilla.org/en/docs/How_to_Turn_Off_Form_Autocompletion
You can set the form element to have autocomplete=”off”
Hooray!
on Thursday 4, 2007
Hi,
I just thought that I’d point out that (in Firefox at least) the autocomplete=”off” attribute is not designed to prevent Firefox from pre-filling the form with your data, but it is there to stop it “guessing” what you are going to type when filling in the form.
Luke
on Thursday 4, 2007
Thanks for the publishing of this article. I am, however, quite annoyed with the fact that I am unable to save form data (mainly logins) when developers put autocomplete=”off” in their form tags. I want to figure out how to bypass the autocomplete=”off” whether or not a developer has it on the form. If you figure it out, that would be a good article as well.