是否有办法检测最后一个导航事件是否是由后退或前进浏览器按钮引起的?
我正在编写一个单页 Web 应用程序,它使用 HTML5 推送状态(回落到哈希标签)来处理客户端导航。
我注意到的一件事是,如果用户向下滚动页面,然后单击指向另一个页面的链接,当他们导航到该页面时,浏览器仍将保持在滚动位置。
我希望如果您进入新页面,它会平滑地滚动到顶部(与所有网站在点击链接时的行为相同)。
我通过导航控制器中的一点 jquery 动画实现了这一点,我现在遇到的问题是,如果您单击浏览器后退按钮,您将不会到达之前所在的滚动位置,而是会位于上一页,但是您将滚动到顶部。
是否可以检测上次/当前客户端导航是否是由浏览器的后退或前进按钮引起的?如果是这样,我将用它来防止滚动。
干杯
I'm writing a single-page web application that uses HTML5 push state (Falling back to hash tags) to handle client side navigation.
One of the things I've noticed is that if a user scrolls down the page and then clicks a link to another page, when they navigate to that page the browser will still remain in the scrolled position.
I wanted to that if you went to a new page it would smooth scroll you to the top (Same behavior as all websites when following links).
I achieved this with a little jquery animation in my navigation controller, the problem I now have is that if you click the browser back button you wont end up in the scrolled position that you were on previously, instead you will be on the previous page but you'll be scrolled to the top.
Is it possible to detect if the last/current client side navigation was caused by the back or forward buttons of the browser? If so I will use this to prevent the scrolling.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您只能捕捉“我的应用程序更改了主题标签”与“浏览器强制更改主题标签”之间的区别。
我是这样检查的:
当您的控制器使用新的主题标签推送新状态(打开页面)时,在将其设置为 window.location.hash 之前,将此新主题标签存储在全局 JavaScript 变量中。
当您捕获“hashchange”事件时,您可以将您的全局变量与 window.location.hash 进行比较。
如果全局变量与新的哈希标签相同,则意味着您的应用程序只是更改了哈希本身(并打开了新页面)。
如果没有设置全局变量,则意味着浏览器强制导航。
但是,您无法知道浏览器强制导航是由于地址栏编辑还是由于后退/前进按钮。
考虑以下代码:
在您的控制器中,在更新哈希标签之前,调用以下代码:
在实际更改 window.location.hashtag 之前调用此代码非常重要!
[编辑]你可以尝试这个替代方案:
将主题标签更改的历史记录存储在 cookie 中并比较更改。根据该信息,您可以估计后退/前进导航事件。
As far as I know, you can only catch the difference between "my application changed the hashtag" versus "the browser forced the hashtag change".
This is how I check it:
When your controller pushes a new state(opens a page) with its new hashtag, store this new hashtag in a global javascript variable right before you set it to window.location.hash.
When you catch the 'hashchange' event, you then compare this global variable of yours with window.location.hash.
If the global variable is the same as the new hash tag, it means your application just changed the hash itself(and opened to a new page).
If the global variable is not set, it means the browser forced the navigation.
However, you cannot know whether the browser forced the navigation because of an address bar edit or because of the back/forward button.
Consider this code:
In your controller, right before you update the hash tag, call this code:
It is very important that this is called BEFORE you actually change the window.location.hashtag!
[edit] You could try this alternative:
Store the history of the hashtag changes in a cookie and compare the changes. From that info you can estimate the back/forward navigation events.
我希望我当前正在开发的网站能够做出相同的响应,无论用户是否输入特殊的 ajax 识别的哈希标签、为其添加书签或单击相应的页面链接。因此,我会查看主题标签本身的模式,并在需要时强制导航。
例如:
i want a site i'm working on currently to respond identically no matter if the user types in the special ajax recognized hash tag, bookmarks it or clicks on the corresponding page link. therefore, i look at the pattern of the hashtag itself and force navigation when needed.
for example: