Quick Inquiry

Home >> Articles >> How to Show Message Box On Window Close Button?

This post will make you learn about the steps that will help you in showing message box on window close button. Asp.net mvc development company experts have intended this post for development community to assist them in web development project. You can read and find the way to show message box or confirm box on window close event.


I seen that many users searches for the examples that shows a message box or information box or confirm box on window close event or when user want to leave a page.


So, the following is the simple example which shows a popup message box when user trying to close a window tab or browser window using top right close button of browser or refresh the page.


<script type="text/javascript"> $(window).bind('beforeunload', function (e) { // Your code and validation if (confirm) { return "Are you sure you want to close this window ?"; } }); </script>

The above java-script shows message box in IE like this. It will display with default message of browser. Chrome, Firefox and other browsers will show different message.


The following second method is used when you have any input controls on form. This code detects form state changed or not. It will use J-Query Form Serialize function, which collects all form inputs and save it in array.


<script type="text/javascript"> // Save form current state on Load of page $('#form').data('serialize', $('#form').serialize()); // If form state change show warning Messagebox, else don't show anything. $(window).bind('beforeunload', function (e) { if ($('#form').serialize() != $('#form').data('serialize')) return "Please confirm that you saved the data on this form."; else e = null; }); </script>

The above java-script shows message box in IE like this.


Asp.net application development India experts have shared this tutorial to make you learn how to show message box on window close button. If you have doubts or want to ask anything related to this topic, make comments and wait for the response of the experts.

Author bio-

Akshita is a content writer who writes on many asp.net related topics and this is one of them. You can share your thoughts regarding this post with her and tell if she has skipped anything important.


For further information, mail us at info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved