Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show alert message when closing a window? [duplicate]

Possible Duplicate:
How to prevent closing browser window?

I want to show an alert message "Please logout before closing the window" with an logout button, when an user tries to close the window and after clicking on logout button the window can be closed. How to achieve this?

like image 468
Mak Avatar asked Nov 05 '12 06:11

Mak


People also ask

How do I get alert messages after redirecting a page?

For displaying alert message after redirection you may use Session or QueryString and on page Load check if the Session or QueryString is not empty then display alert message.

How do I display messages in alert box?

Alert Box. Use the alert() function to display a message to the user that requires their attention. This alert box will have the OK button to close the alert box. The alert() function takes a paramter of any type e.g., string, number, boolean etc.

How to display alert message JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

What is the use of window alert?

window. alert() instructs the browser to display a dialog with an optional message, and to wait until the user dismisses the dialog. Under some conditions — for example, when the user switches tabs — the browser may not actually display a dialog, or may not wait for the user to dismiss the dialog.


1 Answers

Another implementation you can try:

<html>
  <head>
    <script type="text/javascript">
      window.onbeforeunload = function() {
          return "Did you save your stuff?"
      }
    </script>
  </head>
  <body>

  </body>
</html>

Update

As mentioned in comment by @JohnnyFun "Starting with Firefox 44, Chrome 51, Opera 38 and Safari 9.1, generic message will be displayed for different browsers and cannot be customized".

like image 96
Zaheer Ahmed Avatar answered Oct 21 '22 19:10

Zaheer Ahmed