简体   繁体   English

在页面上导航并返回时的pushState

[英]pushState on navigating off page and back

I am trying out popstate and pushState and I am wondering how to handle off page navigation. 我正在尝试popstate和pushState,我想知道如何处理页面导航。

Lets say I have an index page which generates a 'new page' when clicking something and it's loaded in with ajax. 可以说我有一个索引页面,该页面在单击某些内容时会生成一个“新页面”,并且已用ajax加载。 With popstate I change the url. 使用popstate可以更改网址。 In the page loaded are links that go outside of the current 'index' page. 在已加载的页面中,是超出当前“索引”页面的链接。 When somebody presses back they get a dumped state object. 当有人按回去时,他们会得到一个转储的状态对象。

How to prevent it so it actually loads the url that was given during the pushState? 如何防止它实际加载在pushState期间给出的URL?

Thanks in advance. 提前致谢。

It seems you're doing something that causes the browser to replace its cached version of your index page with something else. 看来您正在做的事情使浏览器用其他东西替换了它的索引页面的缓存版本。 Make sure your server sets the 确保您的服务器设置了

Vary: Accept

header when returning the index page and later requests. 返回索引页和以后的请求时的标头。 See this Chromium issue for more information. 有关更多信息,请参见此Chromium问题

Have the link also use an anchor so the browser has a reference to fall back on. 使链接也使用锚点,以便浏览器可以参考。

Throw one of these at the beginning of each of the pages with a unique name attribute. 在每个页面的开头添加具有唯一name属性的其中一个。

So your links would be: 因此,您的链接将是:

<a href="page.html#first">Load the first page</a>

and the page HTML would have this at the top somewhere: 并且页面HTML的顶部将位于以下位置:

<a name="first"></a>

Funny you made this post cuz I've got the exact same problem right now with this site I just started making yesterday: http://asims.fleeceitout.com - had to put arrows everywhere to keep people from gettin lost haha. 有趣的是,您发布了此帖子,因为我昨天才刚开始创建的该站点现在也存在完全相同的问题: http : //asims.fleeceitout.com-必须到处放置箭头,以防止人们迷失哈哈。 I'll end up taking my own advice here but I'm too lazy for now. 我将在这里接受自己的建议,但现在我太懒了。 Plus I'm trying to see how much of the site I can make without a single <a></a> used. 另外,我试图查看不使用单个<a></a>就能创建多少网站。

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

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