I am trying to handle when users click back button in the browser (both desktop and mobile) show them warning, and if they click back second time or confirm they know what are they doing, I let them leave. I have the code bellow which used to work originally on every browsers, but not sure how long ago it stopped working in chrome, still works in Firefox. Basically first time you click on back it shows alert, second time it let users pass... I have try to just add alert into window.onpopstate but it is never shown.
pushShows = 0;
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, './#forward');
window.onpopstate = function() {
if (pushShows == 0) {
alert('Your data will be lost, are you sure you want to leave?');
pushShows = 1;
} else {
history.back();
}
};
}