简体   繁体   English

无论如何,是否有检测上一次导航事件是由后退或前进浏览器按钮引起的?

[英]Is there anyway to detect if the last navigation event was caused by the back or forward browser buttons?

I'm writing a single-page web application that uses HTML5 push state (Falling back to hash tags) to handle client side navigation. 我正在编写一个单页面的Web应用程序,该应用程序使用HTML5推入状态(回退到哈希标签)来处理客户端导航。

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. 我在导航控制器中使用了一个小小的jQuery动画来实现这一点,现在的问题是,如果单击浏览器后退按钮,您将不会回到以前的滚动位置,而是会回到上一页,但是您将滚动到顶部。

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 干杯

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. 当您的控制器使用新的标签推动新状态(打开页面)时,请将此新标签存储在全局javascript变量中,然后再将其设置为window.location.hash。 When you catch the 'hashchange' event, you then compare this global variable of yours with window.location.hash. 当您捕获“ hashchange”事件时,然后将您的全局变量与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! 在您实际更改window.location.hashtag之前,将其称为非常重要!

[edit] You could try this alternative: Store the history of the hashtag changes in a cookie and compare the changes. [edit]您可以尝试以下替代方法:将主题标签更改的历史记录存储在cookie中,并比较更改。 From that info you can estimate the back/forward navigation events. 根据该信息,您可以估算后退/前进导航事件。

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. 我希望我当前正在处理的站点做出相同的响应,无论用户键入特殊的ajax识别的哈希标签,将其添加书签或单击相应的页面链接,都是如此。 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
    }
}

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

相关问题 使用JS检测后退和前进按钮 - Detect back and forward buttons with JS 检测浏览器的前进和后退按钮并触发jquery事件 - Detect browser's forward and back button and trigger jquery event event.preventDefault(); 导航,同时仍允许使用后退和前进按钮 - event.preventDefault(); navigation whilst still allowing back & forward buttons to be used 单独定位浏览器后退和前进按钮 - Target browser back and forward buttons individually 利用Web应用程序的后退和前进浏览器按钮 - Utilizing back and forward browser buttons for web apps 在Angular2中识别后退/前进浏览器按钮 - Recognize Back/Forward browser buttons in Angular2 防止从浏览器后退和前进按钮下载 - Prevent Downloads from browser back and forward buttons 无论如何都要检测用户何时进入主屏幕并返回浏览器? - Is there anyway to detect when a user goes to their homescreen and back into their browser? 如何操纵浏览器历史记录并捕获浏览器后退/前进按钮? - How to manipulate browser history and catch browser back/forward buttons? 如何检测用户是否滑动浏览器后退导航 - How to detect if user swipes for browser back navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM