meloncholy
meloncholy

Reputation: 2192

Stop Chrome autocompleting URL hashes?

I'm having some problems with hashes in Chrome (and Safari). I'm using the hash to store a search string and updating it as its entered. So I only add 1 entry to the history per search, I replace the current history entry as it's typed in.*

location.replace(location.href.slice(0, -location.href.split("#!")[1].length - 2) + searchHash);

This doesn't seem to work in Webkit, but I did manage to work around it with

history.back(1);
setTimeout(function () { location.hash = searchHash; }, 50);

Unfortunately Chrome will often change the URL itself based on my browsing history. So if I've searched for frog (/#!search/frog) before and now start searching for frisking (/#!search/frisking), Chrome will helpfully autocomplete /#!search/fr to /#!search/frog. Mostly this is just confusing for people who want to link to that particular search, but sometimes Chrome has launched a separate part of the site when it decided the search term looked like the hash for one of the other pages.

Chrome autocompleting stuff as I type is often very useful, but I just can't see the value of autocompleting URLs entered by JavaScript. Is there a way of preventing this? Am I doing something wrong here?

* I couldn't use location.hash as Fx decodes it.

Update: If I remove history.back(1), Chrome no longer autocompletes the hash (J. Steen's answer).

Upvotes: 2

Views: 856

Answers (1)

J. Steen
J. Steen

Reputation: 15578

Several sites I've seen this kind of tech implemented at, use a delay in the input so that the location is only updated after, say, the user has stopped typing for 500 milliseconds. That way, there is only the one history entry. The page and search result, meanwhile, is updated instantly as the user types via e.g. ajax.

http://www.prisjakt.nu is a swedish site that utilizes this strategy.

Upvotes: 2

Related Questions