开发者

How to change browser address bar without reloading page - HTML/Javascript [duplicate]

开发者 https://www.devze.com 2023-02-23 01:31 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How does GitHub change the URL but not the reload?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How does GitHub change the URL but not the reload?

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


In short: github uses ajax to load new w开发者_如何学Pythonebpages, without using hashtags. How do they accomplish this?


Any ideas?


By using:

window.history.pushState("string", "Title", "newUrl");

This is new in HTML 5.

Your url will change to newUrl without reloading the page.

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.


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.

https://github.com/browserstate/history.js looks not bad from a quick look at it.

0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消