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?
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.
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.
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With