We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Detect Close Browser in ASP.NET

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage i define a menuItems like "Home", "New" and "Logout". I click  the "New" MenuItem to open a new aspx Page. if I want to close the new page browser tab, I want to show a popup or a dialog that alert the user that he/ she is closing the browser tab. I used the following code in the new aspx page:

 <script type="text/javascript">
        var warnMessage = "Are sure you want to close Application?";
        window.onbeforeunload = function () { return warnMessage };
        $(document).ready(function () {
            $('a').click(function () { window.onbeforeunload = null; });
            $('form').submit(function () { window.onbeforeunload = null; });
        });
    </script>
the problem is that if i press also other buttons or menuItems than the browse closeTab the method works. i would like to know how can i avoid it. i think i need to detect the Syncfusion MenuItem event but i dont know how.

1 Reply

SP Sunil Prabakar C Syncfusion Team May 28, 2013 12:43 PM UTC

Hi Hayssam Hassan,

 

Thanks for using Syncfusion products.

 

To avoid showing alert on clicking menuitem or button we have maintained a flag variable in "Menu" control’s cliendSideevent "ClientSideOnItemSelect"

and "Button" control’s cliendsideevent "OnClientClick" .Therefore, alert will be shown only if the flag is false.

 

Please refer the following code snippet

 

<code>

[JavaScript]

 

<script type="text/javascript">

        var event = 0;       //falg variable

        function buttonClick() {

            event = 1;                //set falg true

        }

        function menuClick() {

            event = 1;                //set flag true

        }

        var warnMessage = "Are sure you want to close Application?";

        window.onbeforeunload = function () {

            if (event === 0) { 

                return warnMessage

            }

 

        };

        $(document).ready(function () {

            $('a').click(function () { window.onbeforeunload = null; });

            $('form').submit(function () { window.onbeforeunload = null; });

        });

    </script>

 

 

[Aspx]

 

  <syncfusion:Menu ID="Menu" runat="server" ClientSideOnItemSelect="menuClick()">

  <asp:Button ID="btn" runat="server" Text="Button Sample" OnClientClick="return buttonClick()" />

 

</code>

 

We have also prepared a simple sample to showcase this behavior and the sample can be downloaded from the link given below

 

MenuDemo.zip

Kindly try the attached sample  and let us know if this helps.

 

Thanks & Regards

Sunil Prabakar C

 


Loader.
Live Chat Icon For mobile
Up arrow icon