-
Notifications
You must be signed in to change notification settings - Fork 687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Browser back/Forward button with saved warning message #609
Comments
IS there any update ? |
What you are trying to do is not as simple as it seems. The browser's back button is a browser-level control, and, to the best of my knowledge, no major browser allows you to cancel or intercept the effects of the back button. // --snip--
// the user changed the form
let state = history.state;
state.marked = true;
history.replaceState(state);
// --snip--
// define newState
history.pushState(newState, null);
// --snip--
// user presses the back button
// --snip--
// we are now in the middleware/route handler
if (history.state.marked) {
let r = confirm("You pressed a Back button! Are you sure?!");
if (r) {
let state = history.state;
delete state.marked;
history.replaceState(state);
history.back();
} else {
history.forward();
}
} Now, of course, the code above is just a very rough estimation of the algorithm. You'd have to do your own research and figure out how best to adapt this approach to your project. But, all in all, I don't think the issue that you've described is inherently related to page.js. |
Hi Team,
I am working on my application and I am stuck onto a solution, where I want to give a warning message on the click of back button of the browser. Whenever there are unsaved changes on the page, and user is navigating away from that page, I want to warn user that there are unsaved changes. So for this I am using below code:
window.addEventListener('popstate', function (event) {
// The popstate event is fired each time when the current history entry changes.
if(dirty==true){
var r = confirm("You pressed a Back button! Are you sure?!");
event.preventDefault()
// Stay on the current page.
history.pushState(null, null, window.location.pathname);
}
}
}, false);
But niether preventDefault() is working nor window.beforeunload nor popstate confirm cancel is giving the desired outcome to come.
However, I created one POC where page.js is not used, and there on click of browser back button on before unload event is giving me warning pop up.
Could you please suggest solution for this as in the documentation no such case is catered
The text was updated successfully, but these errors were encountered: