connectseekers.com

Friday, 27 June 2014

Code to Detect Browser Close Using jQuery In Asp.Net

6/27/2014 - By Pranav Singh 4

In this article I will shoe you code to detect browser close using jQuery in asp.net. In this when when you click on close button of browser on that case it will give and JavaScript alert message that you are going to close the browser.


So for this article first we will create a new asp.net application and add the jquery library reference in your application.

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

Now add the below jquery code in you page header.

<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $(window).bind("beforeunload", function () {
                return confirm("You are about to close the window");
            });
        });
    </script>

In above code we are binding alert message that when user will click on close button you will get the alert message. Here is the complete code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DetectBrowserClose.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Code to Detect Browser Close Using jQuery In Asp.Net</title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script language="javascript" type="text/javascript">
            $(document).ready(function () {
                $(window).bind("beforeunload", function () {
                    return confirm("You are about to close the window");
                });
            });
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Please click on close window
    </div>
    </form>
</body>
</html>

Now view the page in browser.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

4 comments:

  1. This pop is coming not only when the browser is closed but also on the below activities..
    1. Refreshing the Page
    2. Navigating between the pages.
    3. On Click of any link on the page.

    Can I get to fire this event only when the browser is closed? or any other events available to accomplish this?

    ReplyDelete
    Replies
    1. I don't think so. but still i will check and get back to you, if problem is there in code with proper code.

      Delete
    2. Hi This cod is developed for all the scenario which you have mention. In all three cases you are leaving the page.
      if you are navigating on that case also you leaving the page as equal as closing. same for refresh and link click.

      Delete
    3. Thanks Pranav, I was specifically looking for an event that gets fired only when the browser is closed but not during the above scenarios.

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top