Home Ask Login Register

Developers Planet

Your answer is one click away!

Italo Mandara February 2016

before unload is not working as expected

here's my problem, I need to trigger a modal with a call to action when the user tries to get out from a page, I know if you use before unload event you must show an alert returning the text string from the function and that's fine. BUT no matter what I do, I always get the alert before my modal and only when the user has agreed to stay on the page from the built in browser alert.

I've seen in a website, some fronted genius managed to open an html modal right before the alert displays, so you can see the modal already there.

here's the code I'm using:

$(window).bind('beforeunload', function() {
    _('#beforeunload').foundation('reveal', 'open');
    return 'You still have one item in your basket!';

how can i trigger my foundation modal before the browser prompts the user whether to leave in the page or not?


Hakan Kose February 2016


window.onbeforeunload = function(e) {
  return 'Dialog text here.';


Post Status

Asked in February 2016
Viewed 1,769 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life