简体   繁体   English

如何在不重新加载整个页面的情况下刷新页面中的内容并更新URL?

[英]How do I refresh content in the page and update the URL without doing a full page reload?

I thought that JavaScript didn't allow you to update the browser's URL (for bookmarking, etc) without doing a full page refresh. 我以为JavaScript不允许您在不刷新整个页面的情况下更新浏览器的URL(用于书签等)。 Facebook seems to accomplish this in their photos application, though. 不过,Facebook似乎在其照片应用程序中完成了此任务。 When I click "next" to see the next photo, the new photo loads and the URL updates, but the whole page doesn't refresh. 当我单击“下一页”查看下一张照片时,将加载新照片并更新URL,但是整个页面不会刷新。 Does anyone know how they accomplish this? 有谁知道他们是如何做到的?

They don't update the URL per se, they only update the Hash (ie # followed by instructions). 他们本身并不更新URL,而仅更新哈希(即#后跟指令)。 The hash is never sent to the server, but can be used to go to anchor points in the DOM (built in browser functionality), or used as a hook for JavaScript. 哈希永远不会发送到服务器,但可用于在DOM(内置浏览器功能)中定位锚点,或用作JavaScript的挂钩。 Upon page load with a hash variable set, they send an Ajax-request to fetch the corresponding photo. 在页面加载并设置了哈希变量后,它们会发送Ajax请求以获取相应的照片。 The initial page load will, however, load the picture indicated by the url before the #-character. 但是,初始页面加载将加载由#字符之前的url指示的图片。

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

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