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?

Answers


Hakan Kose February 2016

Try;

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

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example

Post Status

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

Search




Leave an answer