Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable autocomplete for all major browsers

How do you disable Autocomplete in the major browsers for a specific input and/or the complete form.

I found this solution:

<input type="text" name="foo" autocomplete="off" />

However, this does not seem to work in all browsers. I've had problems in firefox for instance.

Edit:

My firefox issue has been resolved. That leaves the following: Can I disable autocomplete on a form or do I have to give every input autocomplete="off"

like image 766
Peter Avatar asked Jan 05 '15 11:01

Peter


People also ask

Do browsers ignore autocomplete off?

One point worth mentioning is that many browser will ignore autocomplete settings for login fields (username and password). As the Mozilla article states: For this reason, many modern browsers do not support autocomplete="off" for login fields.

How do I force autocomplete off?

To disable the autocomplete of text in forms, use the autocomplete attribute of <input> and <form> elements. You'll need the "off" value of this attribute.

How do I disable input field autocomplete?

Add autocomplete="off" onto <form> element; Add hidden <input> with autocomplete="false" as a first children element of the form.


2 Answers

Autocomplete should work with the following <input> types: text, search, url, tel, email, password, datepickers, range, and color.

But alas, you could try adding autocomplete='off' to your <form> tag instead of the <input> tag, unless there's something preventing you from doing that.

If that doesn't work, you could also use JavaScript:

if (document.getElementsByTagName) {
    var inputElements = document.getElementsByTagName(“input”);
    for (i=0; inputElements[i]; i++) {
        if (inputElements[i].className && (inputElements[i].className.indexOf(“disableAutoComplete”) != -1)) {
            inputElements[i].setAttribute(“autocomplete”,”off”);
        }
    }
}

Or in jQuery:

$(document).ready(function(){
    $(':input').live('focus',function(){
        $(this).attr('autocomplete', 'off');
    });
});
like image 91
123 Avatar answered Sep 23 '22 23:09

123


You could try manually clearing text fields on page load with javascript, for example:

window.onload = function() {
    elements = document.getElementsByTagName("input");
    for (var i=0; i<elements.length; ++i) {
        elements[i].value = "";
    }
};

This might not work if it's executed before the autofill. Another option is to generate part of the name attributes for your inputs randomly each time the page is rendered (and strip them out when the server handles the submit), then the browser won't try to autocomplete.

See also Is autocomplete="off" compatible with all modern browsers?

like image 43
akxlr Avatar answered Sep 23 '22 23:09

akxlr