简体   繁体   English

Javascript-如果单击“后退”按钮,如何重定向到主页

[英]Javascript - How to redirect to homepage if back button in clicked

I have been doing research and looking at different solutions but can't seem to find one that fits my need. 我一直在研究并寻找不同的解决方案,但似乎找不到适合我需要的解决方案。 I have an AngularJS app that is doing some changes on systems. 我有一个AngularJS应用,正在系统上进行一些更改。 It has many HTML views for showing changes, pushing and removing changes if something goes wrong. 它具有许多HTML视图,用于显示更改,在出现问题时推送和删除更改。 So app goes like this: 所以应用程序是这样的:

Page 1:  Login and get user input
Page 2:  Show changes that user is going to commit
Page 3:  Push changes
Page 4:  Show results
They have an option to remove changes if something does no look right.

I am writing python scripts for performing changes on systems and using Flask for APIs. 我正在编写用于在系统上执行更改并使用Flask for API的python脚本。

Now my issue is if the user is on Page 4 and hits the back button, it goes back to 'push changes' page and actually starts pushing same changes all over again. 现在我的问题是,如果用户位于第4页上并单击“后退”按钮,它将返回到“推送更改”页面,并实际上开始再次重新推送相同的更改。 Is there any way I can modify it so if on any page user hits the back button on the browser, it sends them back to the homepage? 有什么方法可以修改它,以便如果用户在任何页面上单击浏览器上的“后退”按钮,会将它们发送回首页?

Sharing the code won't help as I have many files with tons of code. 共享代码无济于事,因为我有很多文件,而且文件很多。 Here is some overview of my routes and views that might make it a bit clear: 以下是有关我的路线和视图的一些概述,可能会使其更加清楚:

app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "./views/userForm.htm",
        controller : "inputController"
    })
    .when("/checkChanges", {
        templateUrl : "./views/checkChanges.htm",
        controller : "checkChangesController"
    })
    .when("/pushChanges", {
        templateUrl : "./views/pushChanges.htm",
        controller : "pushChangesController"
    })
    .when("/results", {
        templateUrl : "./views/results.htm",
        controller : "resultsController"
    })
    .when("/remove", {
        templateUrl : "./views/remove.htm",
        controller : "removeController"
    })

You can add popstate listener to handle events of user hitting back or forward buttons. 您可以添加popstate侦听器来处理用户popstate后退或前进按钮的事件。

window.addEventListener('popstate', function() {
    $state.go(...); //go to your home state
});

UPDATE : how to redirect to home state 更新 :如何重定向到主状态

Whit the next piece of code, you are creating the component home, setting its HTML template, controller and then, adding a configuration to map it to the URL '/'. 在下一段代码中,您将创建组件主页,设置其HTML模板,控制器,然后添加配置以将其映射到URL'/'。

var home = {
    templateUrl: './templates/home.html',
    controller: 'homeController'
};

angular
    .module('myApp')
    .component('home', home)
    .config(["$stateProvider", function($stateProvider){
        $stateProvider
            .state('home', {
                url: '/',
                component: 'home',
            });
    }]);

Then, in the event listener for the popstate event, you would have $state.go('home'); 然后,在popstate事件的事件侦听器中,您将拥有$state.go('home');

尝试这个;

 window.addEventListener('popstate', () => { //handler });

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

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