visionmedia / page.js

Micro client-side router inspired by the Express router
http://visionmedia.github.com/page.js
7.67k stars 687 forks source link

How to detect page change and prompt user (like window.onbeforeunload)? #553

Open ryangriggs opened 4 years ago

ryangriggs commented 4 years ago

Example: a form that the user fills in. Then they attempt to navigate away from the page. How can we hook the "onbeforeunload" action and prevent page transition unless user approves loss of changes? window.onbeforeunload doesn't seem to fire in these page transitions.

viktorcitaku commented 4 years ago

I'm facing an issue for which I tried to solve with the window.onbeforeunload event, but without success. So basically I have two simple pages that I can navigate to, and the page.js adds to the URL a path based on the current page, in my case, it is the /about page. Now, whenever I hit the refresh button, the action is recognised as an HTTP request leading to page not found. That's why I am trying to intercept this HTTP call with window.onbeforeunload event, which seems not to work.

I've searched for open issues, and I found the following question: #373 . This question doesn't help me in my case but might help you.