window.onPopState() 事件处理程序可以知道导航方向吗? (前进、后退)
我有一个网站,它使用带有pushState()方法和onpopstate()处理程序的ajax导航。
第一次访问主页时,我显示默认内容(没有ajax)。
当有人离开默认页面然后“返回”时,当他们应该返回默认页面时会提供一个空状态。因此,它们停留在网站的第二页而不是第一页(尽管处理程序可以轻松地使用 ajax 将初始内容带回)。
我解决这个问题的唯一方法是在我的初始页面加载上执行pushState,其中包含有关初始内容的信息。通过这种方法,“后退”按钮效果很好!
但是,如果有人在访问 我的网站 之前访问过 www.stackoverflow.com,则会“返回”stackoverflow,然后“前进” ”再次访问我的网站,显示初始内容,但随后再次重新加载,因为“弹出”状态不为空。
那么我可以在 onPopState() 处理程序中判断用户是向后还是向前吗?
i have a site that uses ajax navigation with the pushState() method and an onpopstate() handler.
when first visiting the main page, i display a default content (without ajax).
when someone navigates away from the default page and then goes "back," a null state is supplied when they should be returning to the default page. thus, they stay on the second page of the website instead of the first ( although the handler can easily use ajax to bring the initial content back).
my only way to fix this was to do a pushState on my initial page load with information on what the initial content is. with this method, the "back" button works great!
BUT if someone was at www.stackoverflow.com before going to my site, the goes "back" to stackoverflow and then "forward" to my site again, the initial content is shown, but then reloaded again because the "popped" state is not null.
so can i tell, in the onPopState() handler, whether the user is going backward or forward?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Firefox 中使用 pageshow 事件来捕获初始通过前进和后退按钮进入网站。
I've had some success with using the pageshow event in Firefox to trap that initial entry into the site through forward and back buttons.
有问题。您可以跳过状态(按住后退按钮,选择所需的状态)。这使得事情变得异常困难。
您可以使用 History.js 及其可选的数据持久性来修复该问题。
There is an issue. You can skip states (hold the back button, select the state you want). This makes it incredibly hard.
You can fix that with History.js with it's optional data persistance.