简体   繁体   English

向后浏览器后退按钮

[英]Go one back browser back button

Below is the web page flow 以下是网页流程

OTHER pages ... > START page > TEAM page > PERSON page 其他页面...>开始页面>团队页面>人页面

When the user reach the PERSON page , how can I make the user to go back to the START page instead of going back to the TEAM page when the browser back button is pressed? 当用户到达PERSON page ,如何在按下浏览器后退按钮时使用户返回START page而不是TEAM page

This is what I tried on the PERSON page . 这就是我在“ PERSON page上尝试过的方法。 But when clicked it doesn't move to the START page ? 但是,单击时它不会移至“ START page

window.onhashchange = function() {
    window.history.go(-1);
}

Its simple, You can check if browser back button is clicked or not and If its clicked redirect it to Start Page But Be sure of this because when ever you click back button its always redirect it to your StartPage . 它很简单,您可以检查是否单击了浏览器后退按钮,如果单击了浏览器,则将其重定向到“ Start Page但是请务必确保,因为无论何时单击“后退”按钮,它始终会将其重定向到您的StartPage

Not tested but I think this will do what you want. 未经测试,但我认为这会做您想要的。

 window.onpopstate = function(){
    window.location="StartPage";//you page
   };

To make a back button skip a page in the flow, link to the NEXT page using location.replace. 要使后退按钮跳过流程中的页面,请使用location.replace链接到NEXT页面。 So on the team page: 因此,在团队页面上:

$(function() {
  $(".person").on("click",function(e) { // when clicking
   e.preventDefault();
   location.replace(this.href); // replace team page with person page in the history
  });  
});

assuming 假设

<a href="frank.html" class="person">Frank</a>
<a href="bob.html" class="person">Bob</a>

I guess you could do it by keeping track of the navigation on your website, as a small example, I wrote this fiddle which checks the navigation and if it conforms to team,person,team it will take the user back to the home page 我想您可以通过跟踪您网站上的导航来做到这一点,作为一个小示例,我写了这个小提琴来检查导航,如果它与团队,个人,团队相符,则会将用户带回到首页

I guess the main part here would be to handle the navigation of the user yourself and check if it conforms with what you are expecting. 我想这里的主要部分将是自己处理用户的导航,并检查其是否符合您的期望。 To make sure they can then still access the team site, it would also be good that you clear the navigation in the mean time 为了确保他们仍然可以访问团队网站,最好同时清除导航

function handleNavigation() {
    var loc = document.location.href.split('#')[1];
    if (!loc) {
        loc = 'home';
    }
    console.log(loc);
    toggleClass(curloc, 'inactive');
    toggleClass(loc, 'inactive');
    if (curloc != loc) {
        tracker.push(loc);
        curloc = loc;
        if (tracker.length > 3) {
            tracker.shift();
        }
        if (tracker.join(',') === 'team,person,team') {
            document.location.replace(document.location.href.split('#')[0]);
            return;
        }
    }
}

window.addEventListener('hashchange', handleNavigation);
window.addEventListener('load', handleNavigation);

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

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