简体   繁体   English

HTML5历史记录API:刷新和返回问题

[英]HTML5 History API: Refresh and Back issues

I'm writing code to create a UX similar to how photos are viewed on Facebook from the timeline... 我正在编写代码来创建一个类似于从时间轴上在Facebook上查看照片的用户体验......

  1. Viewing the timeline, you click on the photo and it opens in a modal 查看时间轴,单击照片,然后以模态打开
  2. You refresh and it goes to a dedicated page for the image 您刷新并进入图像的专用页面
  3. You hit back and you return to the timeline 你回击了,然后你回到了时间轴

I have most of this working via the HTML5 History API . 我通过HTML5 History API完成了大部分工作。 Here is what I'm doing when clicking an image... 这是我点击图片时正在做的...

  1. I use preventDefault() to avoid following the link (to the image page) and instead AJAX load a modal with the image 我使用preventDefault()来避免跟随链接(到图像页面)而不是AJAX加载模式与图像
  2. I pushState the URI of the image page so it appears in the address bar pushState图像页面的URI,使其出现在地址栏中
  3. Now I refresh and I'm taken to the dedicated image page (the URI from step #2) 现在我刷新并且我被带到专用图像页面(步骤#2中的URI)
  4. Then I hit the "Back" button, and it shows the previous page URI (ie the timeline) in the address bar but I'm still on the image page. 然后我点击“后退”按钮,它显示地址栏中的上一页URI(即时间轴),但我仍然在图像页面上。

Question: At step #4, is there a way to bypass the history API and just have the browser perform a standard "Back" action so I can return to the previous page (ie the timeline)? 问题:在步骤#4,有没有办法绕过历史API,只是让浏览器执行标准的“后退”操作,这样我就可以返回上一页(即时间轴)?

Have you tried a good ol' fashioned history.go(-1); 你有没有尝试过很好的history.go(-1); ?

I do it like this: 我是这样做的:

step 1 - save the initial page url (which is yet unchanged) 第1步 - 保存初始页面网址(尚未更改)

var savedPageStateURL = window.location.pathname;

step 2 - replace current history state ( not pushing, only replacing) 第2步 - 替换当前历史状态( 推,只替换)

window.history.replaceState( {}, null , url );

step 3 - revert history changes and go back to the first step's state 第3步 - 恢复历史记录更改并返回第一步的状态

if( 'pushState' in window.history && window.history.state )
    window.history.replaceState( {}, null , savedPageStateURL );

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

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