1) I'm trying to find a way to show a confirm pop-up at leaving the page through the browsers back button. The unload event doesn't seem to fire in this case. I'm using angular2. I have tried subscribing to all navigation events:
this.router.events.pairwise().subscribe((e) => {
if (e[0] instanceof NavigationEnd && e[1] instanceof NavigationStart) {
console.log(e[0].id + " " + e[0].url + ", " + e[1].id + " " + e[1].url);
if (!this.whateverService.canNavigateAway) {
//var b = confirm("You have unsaved changes. Navigate away?");
}
}
});
But I can't seem to cancel the navigation.
2) If there is no way to cancel navigating away with the back button, other problems arise. How do I distinguish between back and forward, or going back 3 pages? Then it would be good to know the positions of the pages being navigated to and from in the local browser history. I would like to know for example that there are 7 pages total in the history and we are navigating from the page at index 5 to the page at index 4. That way I could navigate back to where I want without messing up the history.