2

I now have:

window.onbeforeunload = confirmFirst;

function confirmFirst() {   
  return "Leave the page?"; 
}   

This can give users a prompt to confirm that they are leaving the page on all major browsers, except on iOS safari. How can I make it work?

I want to implement this as my users often accidentally swipe right (back button) and left the page after typing in some long paragraphs on my site. Thank you!

user2335065
  • 2,337
  • 3
  • 31
  • 54
  • This is really bad from IOS devices. Such an important event(onunload, pagehide) to track key information before leaving the page. #ios-bugs – webblover Apr 16 '20 at 06:51

2 Answers2

1

What's wrong with your code right now? That should do the job...
You can do this using either regular JS:

window.onbeforeunload = function(){
  return 'Leave the page?';
};

Or even jQuery:

$(window).bind('beforeunload', function(){   
  return 'Leave the page?'; 
});

Is this useful?
Is there an alternative method to use onbeforeunload in mobile safari?

Community
  • 1
  • 1
Cédric D.
  • 27
  • 8
  • 1
    Assuming iOS safari respects the beforeunload event - which it does not. https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5 – mplungjan Jul 07 '15 at 14:44
  • Yeah I looked over the iOS part. – Cédric D. Jul 07 '15 at 14:47
  • I found this: http://stackoverflow.com/questions/6205989/is-there-an-alternative-method-to-use-onbeforeunload-in-mobile-safari – Cédric D. Jul 07 '15 at 14:47
0

window.onbeforeunload = confirmFirst;

function confirmFirst() {   
  if(confirm("Leave the page?")){
    // some code
  }else{
   return false;
  } 
} 
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Keerthi
  • 923
  • 6
  • 22