Open Modal in Tab Closure

Asked

Viewed 191 times

-1

Hello, I would like to open a modal window the moment the user tries to close the browser and ask if they really want to quit.

I know I could do this with beforeunload, but I wanted to control in case it remains not force and appear the banner again.

Thank you.

  • Apart from the fact that it is a bad idea to prevent users from doing what they want (close the browser) there are limitations on some browsers of what you can ask/show: http://answall.com/q/60044/129

  • @Sergio really is a bad thing, but it was what I was asked. About the message I know it is displayed, even so I need to do it. Thanks.

1 answer

1

Create a variable and store it in localStorage;

The first time the user leaves the page, the modal appears, then, at that moment, already saves some data in the localStorage, the next time it is leaving, it checks if the variable exists, if it exists.

Run this code and then try to close the page:

window.onbeforeunload = function(){

if(!localStorage.getItem('sair')){
 	  localStorage.setItem('sair',1)
  return false;	
}
}

Works with Ctrl+F4, alt+F4; Ctrl+w by clicking on the close button of the window.

Browser other questions tagged

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