最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Places overwrite autocomplete off - Stack Overflow

programmeradmin2浏览0评论

I'm using the Google Places API on an address input on my website :

<input type="text" name="street[]" id="street1" autoplete="none" />

But as soon as the Google Places is loaded it replaces my autoplete="none" by autoplete="off". The issue is Google Chrome is not patible with "off" value and keep display the auto fill suggestion. Is there any way to fix this ?

Thanks !

I'm using the Google Places API on an address input on my website :

<input type="text" name="street[]" id="street1" autoplete="none" />

But as soon as the Google Places is loaded it replaces my autoplete="none" by autoplete="off". The issue is Google Chrome is not patible with "off" value and keep display the auto fill suggestion. Is there any way to fix this ?

Thanks !

Share Improve this question asked Sep 6, 2019 at 15:19 magentodevmagentodev 1791 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Here's a method that will work in all browsers:

TL;DR

Rename your input field names and field ids to something non-related like 'data_input_field_1'. Then add the &#8204; character into the middle of your labels. This is a non-printing character, so you won't see it, but it tricks the browser into not recognizing the field as one needing auto-pleting, thus no built-in auto-plete widget is shown!

The Details

Almost all browsers use a bination of the field's name, id, placeholder, and label to determine if the field belongs to a group of address fields that could benefit from auto-pletion. So if you have a field like <input type="text" id="address" name="street_address"> pretty much all browsers will interpret the field as being an address field. As such the browser will display its built-in auto-pletion widget. The dream would be that using the attribute autoplete="off" would work, unfortunately, most browsers nowadays don't obey the request.

So we need to use some trickery to get the browsers to not display the built-in autoplete widget. The way we will do that is by fooling the browser into believing that the field is not an address field at all.

Start by renaming the id and the name attributes to something that won't give away that you're dealing with address-related data. So rather than using <input type="text" id="city-input" name="city">, use something like this instead <input type="text" id="input-field-3" name="data_input_field_3">. The browser doesn't know what data_input_field_3 represents. But you do.

If possible, don't use placeholder text as most browsers will also take that into account. If you have to use placeholder text, then you'll have to get creative and make sure you're not using any words relating to the address parameter itself (like City). Using something like Enter location can do the trick.

The final parameter is the label attached to the field. However, if you're like me, you probably want to keep the label intact and display recognizable fields to your users like "Address", "City", "State", "Country". Well, great news: YOU CAN! The best way to achieve that is to insert a Zero-Width Non-Joiner Character &#8204; as the second character in the label. So replacing <label>City</label> with <label>C&#8204;ity</label>. This is a non-printing character, so your users will see City, but the browser will be tricked into seeing C ity and not recognize the field!

Mission acplished! If all went well, the browser should not display the built-in address auto-pletion widget on those fields anymore!

Hope this helps you in your endeavors!

You can detect when Google Places sets autoplete="off" and override it this way:

const observer = new MutationObserver(function (mutationsList, observer) {
    for (const mutation of mutationsList) {
        if (mutation.attributeName == 'autoplete' && $(mutation.target).attr('autoplete') == 'off') {
            observer.disconnect();
            $(mutation.target).attr('autoplete', 'none');
        }
    }
});

observer.observe(document.getElementById('street1'), { attributes: true });

autoplete = new google.maps.places.Autoplete(document.getElementById('street1'), {
    fields: ['address_ponents'],
    types: ['address'],
});

This issue was previously reported in Google's Public Issue Tracker here: https://issuetracker.google./issues/73783829. You can still ment and star it to receive updates and to increase visibility.

For the meantime, you can try the suggestions in these related posts:

Chrome Autofill covers Autoplete for Google Maps API v3

Stop Google chrome auto fill the input

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论