1

I'm using a button on my app that redirects to the previous page.

It looks like this:

<button ng-click="goBack()">Go back</button>

$scope.goBack = function() {
    $window.history.back();        
}

I want this button to go to the previous URL different of the current URL instead of just the previous page (that can have to the same URL of the current).


Example:

  • If I go to /home, then /products, then /products, and again /products
  • Then I would like to recover /home.

Is there a way to get the first URL different of the current URL?

Mistalis
  • 17,793
  • 13
  • 73
  • 97
  • Possible duplicate of [How do you get the previous url in Javascript?](http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript) – Exception_al Jan 23 '17 at 13:57

3 Answers3

1

You won't have access to the native history urls, for security reasons. But you could use angular to keep your own records in e.g. myHistoryService:

app.run(function($rootScope, $location, myHistoryService) {
    $rootScope.$on('$routeChangeStart', function(ev, next) {
        var path = $location.path();
        myHistoryService.push(path);
    });
});

In myHistoryService you can just push to an array, or use whatever logic you like.

This assumes that you only need the history to work within your own domain.

Buh Buh
  • 7,443
  • 1
  • 34
  • 61
  • I saw things similar on the net, but the `historyService` may grow very quickly, and you will have to set up a cleaning system... It seems a bit complex only for a button of a single page, so I won't use this. Upvoted anyway, because I found your answer interesting. – Mistalis Jan 23 '17 at 14:47
0

If you want to do so only within your angular app, it is not that difficult. You will have to monitor the state changes using the stateChangeSuccess event for example, and count while the state URL is the same. When you want to go back the the last different URL, you can use window.history.go(-X) where X is the number that you counted.

An example (not perfect, just a concept, and for simplicity everything is on the $rootScope).

angular.module('YourModule').run(function() {
    var lastUrl, counter;
    $rootScope.$on('$stateChangeSuccess', function(event, toState) {
        if (lastUrl != toState.url) {
            counter = 1;
            lastUrl = toState.url;
        } else {
            counter++;
        }
    });
    $rootScope.goBack = function() {
        $window.history.go(-1 * counter);
    };
});
Ron Dadon
  • 2,666
  • 1
  • 13
  • 27
-1

1.

You can go make the browser go back to the previous page with window.history.back(); but you cannot access to that URL. If that was possible you could easilly spy every user when they first enter to your website.

But if it is on the same domain you can use document.referrer to access to the previous URL.

2.

For accessing the browser history there is the History Object

You can access it by window.history and it will return an array of history items. And with that object you can compare the current location window.location.href and this previously returned object

Dipiks
  • 3,818
  • 2
  • 23
  • 39
  • (not the downvoter) Tried to `console.log(window.history)`, and get `History { length: 50, scrollRestoration: "auto", state: null }`. How can I access to previous URLs from this object? – Mistalis Jan 23 '17 at 14:08
  • It sure store the history, but you cannot access the URLs as I said, I guess it is for prevent spying. You can use a few method on the History object as described here: https://developer.mozilla.org/en-US/docs/Web/API/History_API – Dipiks Jan 23 '17 at 14:11