停止 Chrome 自动完成 URL 哈希值?
我在 Chrome(和 Safari)中遇到一些哈希问题。我使用哈希来存储搜索字符串并在输入时更新它。因此,我只在每次搜索的历史记录中添加 1 个条目,并替换当前输入的历史记录条目。*
location.replace(location.href.slice(0, -location.href.split("#!")[1].length - 2) + searchHash);
此 似乎在 Webkit 中不起作用,但我确实设法解决了这个问题,
history.back(1);
setTimeout(function () { location.hash = searchHash; }, 50);
不幸的是 Chrome 经常会根据我的浏览历史记录更改 URL 本身。因此,如果我之前搜索过 frog (/#!search/frog
),现在开始搜索 frisking (/# !search/frisking
),Chrome 会自动将 /#!search/fr
自动补全为 /#!search/frog
。大多数情况下,这会让那些想要链接到该特定搜索的人感到困惑,但有时,当 Chrome 认为搜索词看起来像其他页面之一的哈希值时,它会启动网站的单独部分。
Chrome 在我输入时自动完成内容通常非常有用,但我就是看不到 JavaScript 输入的自动完成 URL 的值。有办法防止这种情况吗?我在这里做错了什么吗?
* 我无法使用 location.hash 作为 Fx 对其进行解码。
更新:如果我删除 history.back(1)
,Chrome 将不再自动补全哈希值 (J. Steen 的回答)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我见过一些实施这种技术的网站,它们在输入中使用延迟,以便仅在用户停止输入 500 毫秒后才更新位置。这样,就只有一个历史条目。同时,页面和搜索结果会在用户通过 ajax 等键入时立即更新。
http://www.prisjakt.nu 是一个采用此策略的瑞典网站。
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.