question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router: ability to intercept back button or tab close that would terminate the app

See original GitHub issue
[ ] bug report
[x] feature request
[ ] support request

Current behavior Cannot intercept when the back button or closing the current browser tab will close the app w/o a warning.

Expected behavior Should be able to detect both actions and take appropriate actions. Could be done with router hook or some other means.

I understand that browsers differ in how they handle this but the router should have a way to cope. Barring that, we need documentation to cope.

Minimal reproduction of the problem with instructions

  1. Launch routing app running on localhost (e.g., http://localhost:3000)
  2. Paste another site into address bar (e.g., google.com)
  3. Paste the locally running app into address bar (e.g., http://localhost:3000)
  4. Hit the back button

You’ll be taken to google.com w/o interruption.

What is the motivation / use case for changing the behavior?

Don’t lose work or context by accidental over-clicking of back button or closing the tab.

Please tell us about your environment:

  • Angular version: 2.2.0
  • Browser: all
  • Language: all

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewdenobregacommented, May 1, 2017

The behavior for window.onbeforeunload changed between 2 and 4:

  • In 2 binding to window.onbeforeunload works as expected, and shows a dialog if the handler returns false.
  • In 4 the function bound to window.onbeforeunload runs, but navigation happens regardless of what is returned.

I can’t demonstrate this in a plunkr because plunkr binds its own handler to window.onbeforeunload.

The canDeactivate hook is not run when the page is reloaded, so I can’t see a way to block page reloads, for example if there are unsaved changes, in Angular 4. This is something that should be supported as per the original issue. @wardbell

1reaction
skreborncommented, Nov 24, 2016

Should window.onbeforeunload be used for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Back Button on a Progressive Web Application closes ...
First up, when there's no browser history for the page that the user is on, pressing the back button immediately closes the app....
Read more >
Using Router Events To Detect Back And Forward Browser ...
Hi, as soon as I am pressing back button on landing page, it navigates away to default browser page. The app exits without...
Read more >
Preventing going back - React Navigation
The user closes the app (e.g. by pressing the back button on the home screen, closing the tab in the browser, closing it...
Read more >
NET MAUI Shell navigation - Microsoft Learn
NET MAUI Shell apps can utilize a URI-based navigation experience that permits ... Navigation events; Navigation deferral; Pass data; Back button behavior.
Read more >
Provide custom back navigation - Android Developers
For example, when using a WebView , you might want to override the default Back button behavior to allow the user to navigate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found