window.history.back returning only one page

0

I'm having problem with window.history.back() because it's returning only one page.

I use the following function:

        <script type="text/javascript" charset="utf-8">

        // Wait for device API libraries to load
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        // device APIs are available
        //
        function onDeviceReady() {
            // Register the event listener
            document.addEventListener("backbutton", onBackKeyDown, false);
        }

        // Handle the back button
        //
        function onBackKeyDown() {
            if($('.upage:visible').attr("id") == 'listar_CELULAS'){
                var r=confirm("Você realmente deseja sair do aplicativo ?");
                if(r==true){
                    navigator.app.exitApp();
                }else{

                }
            }else{
                console.log("ta entrando aqui");
                window.history.back();
            }
        }

    </script>

I need all to return to the starting point of the stack. To put in the history stack I use this function:

function activate_page(sel, have_state)
{
    var $dn = $(sel);
    var is_vis = $dn.is(":visible");
    if(!is_vis)
    {
        $dn.parents("body").find(".upage").addClass("hidden");
        $dn.removeClass("hidden");
    window.history.pushState({upage:sel}, sel, document.location.origin + document.location.pathname +sel);
    $(document).trigger("pagechange");
    }
}

Probably the ionic is part in this problem, in fact not to rightly understanding the moment that everyone is doing something, follow my ionic code:

angular.module('ionic')
  .run(function($ionicPlatform,$ionicPopup, $state,$ionicHistory){
    $ionicPlatform.registerBackButtonAction(function (event) {
      //console.log(window.history);
      if($state.current.name=="app.listar_CELULAS"){
        alert("entro aqui");
        navigator.app.exitApp();
      }
      else {
        //window.history.back();
      }
    }, 100);
});

Here is the function that theoretically populate the history of visited pages:

function activate_page(sel, have_state)
{
    var $dn = $(sel);
    var is_vis = $dn.is(":visible");
    if(!is_vis)
    {
        $dn.parents("body").find(".upage").addClass("hidden");
        $dn.removeClass("hidden");
        window.history.pushState({upage:sel}, sel, document.location.origin + document.location.pathname +sel);
        console.log(window.history);
        $(document).trigger("pagechange");
    }

}

Where is the error?

Thanks for the time being.

    
asked by anonymous 23.12.2015 / 19:27

1 answer

10

The windows.history.back() returns only one page, if you want to return more pages you should use windows.history.go(-2) for example.

  • This history.go(-1) equals this history.back()
  • This will return two pages: history.go(-2)

Negative history.go(-x) goes back and positive history.go(x) advances the specified amount (changing x to an integer).

Using Cordova events

The document.addEventListener("backbutton" ...) button itself is already a return event, you do not have to use window.history.back again, the page is already coming back, just do this:

    function onBackKeyDown() {
        if($('.upage:visible').attr("id") == 'listar_CELULAS'){
            var r=confirm("Você realmente deseja sair do aplicativo ?");
            if(r == true){
                navigator.app.exitApp();
            }else{

            }
        }else{
            console.log("Voltou uma página");
        }
    }

Coming back to the beginning

There is also the history.length property, with it you can know how many pages have already been added in the total and make apply to the history.go(x) parameter, apparently the number of pages passed in it has to either an exact number minus the current page , that is if you navigated 20 times and using history.go(-50) nothing will happen. So try this:

window.history.go(-(window.history.length - 1));

Documentation:

Example usage:

  • First click the Page button as many times as you like and note the browser's navigation buttons (here on the same site).
  • Then click on the Back to start button and look again at the browser's navigation buttons.

function inicio() {
    window.history.go(-(window.history.length - 1));
}

function paginar() {
    var current = parseInt(String(window.location.hash).replace(/^#/, ""));

    if (isNaN(current)) {
        window.location.hash = "1";
    } else {
        window.location.hash = ++current;
    }
}
<button onclick="paginar();">Páginar</button>
<button onclick="inicio();">Voltar ao começo</button>
    
23.12.2015 / 19:33