user1218631
user1218631

Reputation:

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?

Upvotes: 0

Views: 433

Answers (1)

Hakan Kose
Hakan Kose

Reputation: 1656

Try;

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

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

Upvotes: 1

Related Questions