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).