简体   繁体   English

如何在不重新加载页面的情况下更改地址栏中的URL?

[英]How do you change the URL in the address bar without reloading the page?

How does Shopify do this? Shopify如何做到这一点? Go to their website, click on the Features link and you'll see that the URL in your browser's address bar says: 转到他们的网站,点击功能链接,您会看到浏览器地址栏中的URL显示:

http://www.shopify.com/tour/sell-online http://www.shopify.com/tour/sell-online

Then click on any of the sub links and you'll see that the URL in the address bar changes without using a hash and there is no page flip. 然后单击任何子链接,您将看到地址栏中的URL更改而不使用哈希,并且没有页面翻转。

I don't think they are using ajax to change the content because it all appears to be included in hidden divs on the page, but regardless, you can apparently change the URL using client side tricks. 我不认为他们使用ajax来更改内容,因为它似乎都包含在页面上的隐藏div中,但无论如何,您显然可以使用客户端技巧更改URL。 Your help is appreciated? 感谢您的帮助?

You use the new HTML5 history API to push a new state. 您使用新的HTML5历史记录API来推送新状态。

Here's the MDN documentation and a good tutorial . 这是MDN文档一个很好的教程

Beware that doing this is often painful (you have to manage correctly the state of your application) and it doesn't work with IE9 . 请注意,这样做通常很痛苦(您必须正确管理应用程序的状态),并且它不适用于IE9 It's almost always combined with ajax : it's the solution to let dynamically loaded content be bookmarkable even while the whole page isn't reloaded or changed. 它几乎总是与ajax结合使用:即使整个页面没有重新加载或更改,它也是允许动态加载的内容可加入书签的解决方案。

查看pushState ,但请注意,并非所有浏览器都支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM