如何在不重新加载页面的情况下更改浏览器地址栏 - HTML/Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5607902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change browser address bar without reloading page - HTML/Javascript
提问by RadiantHex
Possible Duplicate:
How does GitHub change the URL but not the reload?
可能的重复:
GitHub 如何更改 URL 而不是重新加载?
Hi folks,
嗨伙计,
I have noticed that Github.com does not force users to download webpages when browsing repositories, but instead uses AJAX in order to refresh the page content.
我注意到 Github.com 在浏览存储库时不会强制用户下载网页,而是使用 AJAX 来刷新页面内容。
Github also changes the browser address bar to the real address representing the content. I have no idea how to implement this if not by using # hashtags within the url.
Github 还将浏览器地址栏更改为代表内容的真实地址。如果不是通过在 url 中使用 # hashtags,我不知道如何实现这一点。
In short:github uses ajax to load new webpages, without using hashtags. How do they accomplish this?
简而言之:github 使用 ajax 加载新网页,而不使用主题标签。他们是如何做到这一点的?
Any ideas?
有任何想法吗?
回答by euphoria83
By using:
通过使用:
window.history.pushState("string", "Title", "newUrl");
This is new in HTML 5.
这是 HTML 5 中的新功能。
Your url will change to newUrl without reloading the page.
您的 url 将更改为 newUrl,而无需重新加载页面。
Note: title arg in the method will not change the Title of the html page. This is used to name the page in the browser history, incase u go back and then go forward.
注意:方法中的 title arg 不会改变 html 页面的 Title。这用于在浏览器历史记录中命名页面,以防您返回然后前进。
回答by ThiefMaster
Have a look at the HTML5 history api. If you plan to use it, getting a library wrapping it and maybe even adding a fallback to location hashes would be a good idea.
看看HTML5 history api。如果你打算使用它,让一个库包装它,甚至可能添加一个回退到位置哈希将是一个好主意。
https://github.com/browserstate/history.jslooks not bad from a quick look at it.
https://github.com/browserstate/history.js快速浏览一下,看起来还不错。