Browsing history stuck in loop

Asked

Viewed 23 times

0

I have two links, one is intro and the other reading:

  1. /system/intro/1373/ninth-ninth (Introduction booklet)
  2. /system/reading/1373/ninth-ninth-ninth (Reading booklet)

Navigation would be in this sequence: Home > Introduction notebook (button) > reading notebook When there’s a history, he should come back: Reading Book > Introduction Notebook (button) > (Home Page)

The problem is that the history got stuck between: Reading Booklet > Introduction Booklet (button) > Reading Booklet

Explanation:

In the mobile version of my application, there is a back button inside the introduction page of an activity notebook that should go back to the home or the previous page that was stored in the browsing history, this rule is valid if there is a history, however, there is an exception which is when the origin of access is a return of the reading notebook.

The problem is that I can not put a fixed navigation, I need to use history, this because it can access both the introduction notebook, or the reading notebook typed either in the browser, or through a link at home.

To control the routes, in Angular init I have a method that is called every time there is a URL exchange, in this method is saved in localStorage the current URL, whenever there is a link change this value is rewritten, to keep the history of the previous URL:

$rootScope.$on("$locationChangeStart", function(event, next, current) { 
    if (localStorage.getItem('nav_page') == null || (localStorage.getItem('nav_page') != null && localStorage.getItem('nav_page') !== current)) {
        //grava sempre que mudar absUrl...
       localStorage.setItem('nav_page', current); 
   } 
});

And also another method that creates the default browsing state history with pushState each time a route is started:

$rootScope.$on('$routeChangeStart', function (e, current, pre) {

    try {

        history.pushState(null, document.title, location.href);     
    } catch(e) {
    }
});

I have the following script that is used on the button of the introduction page in your controller of this page, which redirects to the correct path when it is not the same origin and nor the reading notebook, but to cases that it is falling in the history, as a refresh for example, and when it comes to this situation, it uses the history to return, this is usually happening when I am accessing the reading notebook and go back to the intro, then it loops and keeps going back to the notebook, leaving the user stuck in this condition endlessly:

$scope.goToPage = function() {
   
    if (localStorage.getItem('nav_page') != null) {
        var redirect_path = localStorage.getItem('nav_page');
        var pathname = redirect_path.match(/(http[s]?:\/\/)?([^\/\s]+\/sistema)(.*)/)[3];
        var path_current = window.location.pathname.match(/\/([^\/sistema].*)/)[0];

        if (redirect_path != null && pathname !== path_current && pathname.indexOf('/leitura/') == -1) {
            $location.path(pathname);
        } else {
        
            if (history.length) {
                localStorage.setItem('nav_page', path_current);
                history.back();
                window.onpopstate = function (event) {
                    $location.path('/home');
                }
                $location.path('/home');

                //caso de bug no history.back, força um redirect para home
                $timeout(function(){
                    $location.path('/home');
                }, 2000);

            } else {
                $location.path('/home');
            }
        }
    } else {
        $location.path('/home');
    }
}
function notIsGame() {
    var referrer = $document.getReferrer();
        return (referrer && referrer.indexOf('/play/') == -1 && 
        referrer.indexOf('/atividade/') == -1 || referrer == "");
}
   

In intro HTML I have a button:

<button ng-click="goToPage()">Voltar</button>

How could it improve, so that the intro can always return from any origin without being in the reading book.

I have, I don’t know if it helps, this control variable that tells me I came from the reading notebook:

$rootScope.has_book_open = true;
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.