-1

Is it possible to trigger a function when a using closes the browser tab and prevent it from closing instead a popup will ask the user to proceed to other page or continue to close the browser page?

Scenario:

When the user closes the browser tab of a page, then it wont close right away instead a custom popup (possibly html popup) will prompt and ask if continue to close or proceed to other page. So it would have 2 buttons, "Close" and "Proceed to Page".

If it's not possible using HTML popup, prompt would be fine too. But it should be crossbrowser.

By the way, i prefer using Javascript.. So that's my ideal script to make this function work

Thanks :)

PHP Noob
  • 1,597
  • 3
  • 24
  • 34

2 Answers2

2

You're looking for onbeforeunload:

window.onbeforeunload = function() {
  return "Are you sure you wish to leave the page?";
}

Just know that you will really annoy your visitors if you do this, it is strongly disliked.

jszobody
  • 28,495
  • 6
  • 61
  • 72
0

Try this:

     var popit = true;
     window.onbeforeunload = function() { 
      if(popit == true) {
           popit = false;
           return "Are you sure you want to leave?"; 
      }
 }

Working Fiddle

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125