Code: Select all
window.addEventListener('beforeunload', function (e) {
const thankYouVisible = document.querySelector('.thank-you')?.offsetParent !== null;
if (!thankYouVisible) {
const confirmationMessage = "Are you sure you want to leave this page? Your
progress may be lost.";
e.preventDefault();
e.returnValue = confirmationMessage;
return confirmationMessage;
}
});
Jetzt ist unter dem Code für iOS safari/crome
Ausgabe ist der erste Code für Desktop für Desktop, aber jetzt Fall von iOS/Android.
Code: Select all
window.addEventListener('pagehide', () => {
const thankYouVisible = document.querySelector('.thank-you')?.offsetParent !== null;
if (!thankYouVisible) {
localStorage.setItem('triedToLeave', '1');
}
});
window.addEventListener('DOMContentLoaded', () => {
if (localStorage.getItem('triedToLeave') === '1') {
localStorage.removeItem('triedToLeave');
const shouldRefresh = confirm('It looks like you tried to leave the page. Do you want to refresh?');
if (shouldRefresh) {
location.reload(); // Refresh only if user clicks "OK"
}
}
});