-3

I have the popup appearing but the page continues to unload. How can I prevent the page from unloading until either the .js-close is clicked or the form in my popup is submitted?

$(document).ready(() => {
    window.onbeforeunload = () => {
        const newsSubscribePopup = $('#news-subscribe-popup');

        if (newsSubscribePopup.length) {
            $.magnificPopup.open({
                items: {
                    src: newsSubscribePopup,
                },
                type: 'inline',
                preloader: true,
                modal: true,
                showCloseBtn: false,
            });
        }
    };
});`
DumbDevGirl42069
  • 891
  • 5
  • 18
  • 47
  • 1
    This is not how beforeunload works. You are supposed to cancel the event, and return a _text string_ as message to show to the user in the confirmation dialog. This dialog is created by the browser itself, you are not supposed to try and show your own HTML modals, popups or stuff like that using it. – misorude Jan 09 '19 at 08:21

1 Answers1

0

You can use the confirm() function with onclick handler.
Here are simple example:

<div class="js-close" data-confirm="Are you sure to delete this item?">action</div>

$('.js-close').on( "click", function() {

      var choice = confirm($(this).attr("data-confirm"));

      if (choice) {
        alert('ok');
      }else{
        alert('no');
      }

});

http://jsfiddle.net/spbjqvgw/

Ingus
  • 1,026
  • 12
  • 34