How can I check the url before calling history.back()?

67 views Asked by At

Basically I want to check if history.back() which I call on button click takes me to an external page and block the button in this case

1

There are 1 answers

0
Tal Rofe On

The short answer is that you cannot get the previous external web url. Check out this history API: https://developer.mozilla.org/en-US/docs/Web/API/History

However, if the external page is not important by its value, but only for being external one, you could trick it. Just preserve a route state when navigation across your application.

When a user navigates from your app to other page in the app, set a state indicating that the previous page was internal one. Then, once a user clicks your button, check that state and act according to its value.

Example:

Pass data on navigation:

this.router.navigate(['/some-internal-page', { internal: true }]);

Read data:

this.isInternal = this.route.snapshot.paramMap.get('internal'));

Then, when a user clicks the button, act in accordance to this.isInternal value.