Search code examples
javascriptjquerypopupwindowonbeforeunload

Jquery Exit Popup meanwhile trigger a function


My goal is to trigger a pop up function meanwhile the user is going out from my website.

So I want to show a exit popup and in the background show my pop up at the same time.

I already have found different code on internet but no one works well.

Code :

<script type="text/javascript">
  function PopUp() { return 'Do you wnat to leave my site?'; }
  function UnPopIt() { /* no data */ }

  $(document).ready(function() {
  window.onbeforeunload = PopUp;
  });
</script>

Is it possible to trigger a function like a pop up in the background ? I am trying this code but it doesn't work :

function PopUp()
{
    alert ('Do you wnat to leave my site?'); triggerpopup();
}

Solution

  • 1,not very understand your needs,but i can't add a comment right now.
    2,suppose you want a selfdefined pop(not another alert),then this goes well

    function PopUp() { 
      triggerpopup();
      return 'Do you?';
    }
    
    function triggerpopup() { 
      console.info('called with default pop') 
    }
    
    $(document).ready(function() {
      window.onbeforeunload = PopUp;    
    });