Display a <div> when you click the close of the browser window

Asked

Viewed 618 times

3

Does anyone know how I would display a <div> which is hidden when the user clicks the close button on the browser window?

  • I did the test and the page displayed the div and closes, it would be possible not to close the page, but only to display the div?

  • no, unless you create a browser plugin that does this.

  • 2

    What is done is different, show before closing, when leaving with the mouse at the top of the screen. By the way, a very boring thing. (If interested, do a search, this has already been asked on the site).

  • I agree, but I need to display a div with a message saying that the user has not filled in all the data if he really wants to close the browser.

  • I believe you could display an Alert or something before leaving

  • So, it is easier to warn you when leaving with the mouse, because it gives more time to see. Maybe for your case it is the case to ask if it confirms to close. And this already has answer on the site too, how to ask for confirmation.

  • It would be very nice, could guide me how to produce?

  • Here: http://answall.com/questions/62571/70

  • She also has: http://answall.com/questions/71128/70

  • I posted a solution, but as @Bacco reported, there are already several solutions to this problem in Sopt or OS

  • 1

    @Munirbaarini I know you have already been pointed out, but I would like you to take a look at my answer http://answall.com/a/113155/3635, in which I explain how unonload/beforeunload actually work and how facebook-like websites do to display a "div".

  • I understand William, so it is not possible to customize the modal.

Show 7 more comments

1 answer

1


In case you could run a javascript that displays an Alert or modal for this, eg:

<script>
window.onbeforeunload = function (event) {
  var message = 'Você tem certeza que deseja sair?';
  if (typeof event == 'undefined') {
    event = window.event;
  }
  if (event) {
    event.returnValue = message;
  }
  return message;
}

</script>

Documentation about the beforeunload event.

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

  • I need to display a custom window, so I thought of a <div>

  • make the modal is quiet, the problem is to call it when click on the close button of the browser and only display the modal without closing the browser

  • there are some limitations that one can perform in the beforeunload, have to see if it allows to run a modal within it.

  • Staff thank you very much all!

  • @Munirbaarini follows event documentation https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

  • Thank you very much Jefferson!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.