我发现了很多关于更改网址(无需重新加载)的问题。一些答案是 - 使用插件,使用location.hash
...,或重新加载
但他们都没有为我工作。
在网站上我有一个下拉菜单,并且在其更改时 url 参数应该已更改。
所以我想做的是:
我想改变:www.foo.com?country=Germany
变成 www.foo.com?country=Slovenia
不重新加载。
我正在努力实现的目标是可能的吗?
我发现了很多关于更改网址(无需重新加载)的问题。一些答案是 - 使用插件,使用location.hash
...,或重新加载
但他们都没有为我工作。
在网站上我有一个下拉菜单,并且在其更改时 url 参数应该已更改。
所以我想做的是:
我想改变:www.foo.com?country=Germany
变成 www.foo.com?country=Slovenia
不重新加载。
我正在努力实现的目标是可能的吗?
您可以在较新的浏览器中;在旧版本中,您只能更改哈希值。这似乎是一篇关于该主题的好文章:http: //html5doctor.com/history-api/
What you are looking for is the History
API HTML5 provides. It comes with functionality like history.pushState(...)
, history.popState(...)
, which lets you dynamically change the URL without having to assign a new URL altogether.
It is used by many sites, including, I suspect, Facebook itself, where if you open a chat box, and navigate between pages, the chat box doesn't reload. It means that all new content are being fetched through Ajax, but then the URL won't change, would it? But it does. I think they do it through history.pushState(...)
, where you just push a new state into the History stack, and it changes only a certain part of the page. You will find an excellent tutorial here.