2
  var inFormOrLink;
$('a').on('click', function () { inFormOrLink = true; });
$('form').on('submit', function () { inFormOrLink = true; });

$(window).on('beforeunload', function (eventObject) {
  var returnValue = undefined;
  if (!inFormOrLink) {
    returnValue = "Do you really want to close?";
  }
  if (returnValue != undefined) {
    eventObject.returnValue = returnValue;
    return returnValue;
  }
});

Ref : Browser close event

I tried to execute the above mentioned code in Chrome Version 65.0.3325.181 it is working properly i.e popup does not open while redirecting or submitting form, i want to show popup only when user close the tab, sometimes browser shows the popup but sometimes tab gets closed without showing popup. I don't know why it is happening.

ANEES
  • 300
  • 3
  • 16
  • 1
    @LGSon I don't have to store any value,I just have to show a popup which will contain whishlist's data (if there is any), else tab will be closed. – ANEES Apr 20 '18 at 07:33
  • In those cases where it isn’t working as you expected, have you checked what `inFormOrLink` contains at that moment? – CBroe Apr 20 '18 at 08:49

0 Answers0