是否有办法检测最后一个导航事件是否是由后退或前进浏览器按钮引起的?

发布于 2024-12-18 20:18:55 字数 344 浏览 1 评论 0原文

我正在编写一个单页 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-12-25 20:18:56

据我所知,您只能捕捉“我的应用程序更改了主题标签”与“浏览器强制更改主题标签”之间的区别。

我是这样检查的:

当您的控制器使用新的主题标签推送新状态(打开页面)时,在将其设置为 window.location.hash 之前,将此新主题标签存储在全局 JavaScript 变量中。
当您捕获“hashchange”事件时,您可以将您的全局变量与 window.location.hash 进行比较。
如果全局变量与新的哈希标签相同,则意味着您的应用程序只是更改了哈希本身(并打开了新页面)。
如果没有设置全局变量,则意味着浏览器强制导航。
但是,您无法知道浏览器强制导航是由于地址栏编辑还是由于后退/前进按钮。

考虑以下代码:

// Global hashtag variable, used to compare state changes
var gCurrentHash = window.location.hash;

// Called when hashtag is changed by the browser
function onHashChange(event)
{
    var hash_tag = window.location.hash;

    if (hash_tag != gCurrentHash)
    {
        // Browser forced navigation
    }
    else
    {
        // App caused navigation
    }
}

window.addEventListener('hashchange', onHashChange, false);

在您的控制器中,在更新哈希标签之前,调用以下代码:

gCurrentHash = 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:

// Global hashtag variable, used to compare state changes
var gCurrentHash = window.location.hash;

// Called when hashtag is changed by the browser
function onHashChange(event)
{
    var hash_tag = window.location.hash;

    if (hash_tag != gCurrentHash)
    {
        // Browser forced navigation
    }
    else
    {
        // App caused navigation
    }
}

window.addEventListener('hashchange', onHashChange, false);

In your controller, right before you update the hash tag, call this code:

gCurrentHash = window.location.hash;

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.

树深时见影 2024-12-25 20:18:56

我希望我当前正在开发的网站能够做出相同的响应,无论用户是否输入特殊的 ajax 识别的哈希标签、为其添加书签或单击相应的页面链接。因此,我会查看主题标签本身的模式,并在需要时强制导航。

例如:

var myDefaultPageName = "myAjaxPage1";

// hash tags in this domain may indicate ajax page navigation
// using '#!' to indicate ajax links however, allowing user to type in 
// hashtag without '!' so function understands both '#!myAjaxPage1' and
// '#myAjaxPage1' as ajax requests, the pattern of the hashtag will be
// checked to determine if ajax navigation is needed

function processHashtagChange() {
    // note: bookmarked urls may be #! or #, need to understand both
    var startIndex = 1; // skip '#'
    if (location.hash.charAt(1) == '!') { // check for special '#!' ajax prefix
        startIndex++;
    }

    // extract ajax pagename from hash
    var pagename = location.hash.substring(startIndex); 

    var regex = "."; // match everything for now, hash tags 
                     // only used for ajax navigation here

    if (pagename.length == 0) // default to something if needed, allows 
    {                         // back button to http://mydomain.com to load 
                              // default ajax page
        pagename = myDefaultPageName;
    }

    if (pagename.match(regex)) { // does this hash indicate ajax navigation
        var pageurl = '/?page=' + pagename;    
        loadPageViaAjax(pageurl); // update the dom via 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:

var myDefaultPageName = "myAjaxPage1";

// hash tags in this domain may indicate ajax page navigation
// using '#!' to indicate ajax links however, allowing user to type in 
// hashtag without '!' so function understands both '#!myAjaxPage1' and
// '#myAjaxPage1' as ajax requests, the pattern of the hashtag will be
// checked to determine if ajax navigation is needed

function processHashtagChange() {
    // note: bookmarked urls may be #! or #, need to understand both
    var startIndex = 1; // skip '#'
    if (location.hash.charAt(1) == '!') { // check for special '#!' ajax prefix
        startIndex++;
    }

    // extract ajax pagename from hash
    var pagename = location.hash.substring(startIndex); 

    var regex = "."; // match everything for now, hash tags 
                     // only used for ajax navigation here

    if (pagename.length == 0) // default to something if needed, allows 
    {                         // back button to http://mydomain.com to load 
                              // default ajax page
        pagename = myDefaultPageName;
    }

    if (pagename.match(regex)) { // does this hash indicate ajax navigation
        var pageurl = '/?page=' + pagename;    
        loadPageViaAjax(pageurl); // update the dom via ajax
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文