beforeunload event of jquery
<p>These are my javascript function to check whether the user exiting the application with out saving the data</p>

<pre><code> &lt;script src="jquery-1.4.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="JScript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="JScript2.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="jquery.alerts.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;link href="jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" /&gt;

&lt;script type="text/javascript"&gt;
    var isDirty;
    isDirty = 0;

    function setDirty() {
        var form = document.getElementById('form1');
        var inputs = form.getElementsByTagName('input');
        var e = document.getElementById("ddl");
        var strUser = e.options[e.selectedIndex].value;
        var elems = document.getElementsByTagName('input');
        for (var i = 0; i &lt; elems.length; i++) {
            if (elems[i].type == 'text' || elems[i].type == 'checkbox' || elems[i].type == 'radio') {
                is_checked = elems[i].checked;
                is_checked1 = elems[i].checked;
                if (elems[i].value == '' &amp;&amp; !(is_checked) &amp;&amp; !(is_checked1) &amp;&amp; strUser == '0') {
                    isDirty = 0;
                    //alert("In");
                }
                else {
                    isDirty = 1;
                }
            }
        }
    }
    $(document).ready(function f() {
        alert("In");
        $("#Button1").click(function () {
            if (isDirty == 1) {
                jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
                    if (r == true) {
                        //var link = '&lt;%=btnDelete.UniqueID %&gt;'
                        //__doPostBack('&lt;%=btnDelete.UniqueID%&gt;', "");
                    }
                    else
                        return false;

                });
                //return false;
                return false;
            }
            else {
                // __doPostBack('&lt;%=btnDelete.UniqueID%&gt;', "");
            }
        });
    });



&lt;/script&gt;
&lt;script type="text/javascript"&gt;

    $(window).bind('beforeunload', function () {
        if (isDirty == 1) {
            alert("Hi");
            jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
                if (r == true) {
                    //var link = '&lt;%=btnDelete.UniqueID %&gt;'
                    //__doPostBack('&lt;%=btnDelete.UniqueID%&gt;', "");
                }
                else
                    return false;

            });
            //return false;
            return false;
        }
        else {
            // __doPostBack('&lt;%=btnDelete.UniqueID%&gt;', "");
        }
    });

&lt;/script&gt;
</code></pre>

<p>This is my design</p>

<pre><code>&lt;div&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:TextBox ID="txt" runat="server"&gt;&lt;/asp:TextBox&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:RequiredFieldValidator ID="Rqrd" runat="server" ValidationGroup="g" ControlToValidate="TextBox1"
                    ErrorMessage="Required"&gt;&lt;/asp:RequiredFieldValidator&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:CheckBox ID="chk" runat="server" Text="Check Me" /&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:RadioButton ID="radio" runat="server" Text="Radio" /&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:DropDownList ID="ddl" runat="server"&gt;
                    &lt;asp:ListItem Text="--Select--" Value="0"&gt;&lt;/asp:ListItem&gt;
                    &lt;asp:ListItem Text="One" Value="1"&gt;&lt;/asp:ListItem&gt;
                    &lt;asp:ListItem Text="Two" Value="2"&gt;&lt;/asp:ListItem&gt;
                &lt;/asp:DropDownList&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;input id="Button2" type="button" value="Submit" /&gt;
            &lt;/td&gt;
            &lt;td style="display: none; font-size: 0px; width: 0px;"&gt;
                &lt;asp:LinkButton ID="btnDelete" runat="server" Text="Ok" ValidationGroup="g" OnClick="btn1_Click" /&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:Button ID="Button1" runat="server" Text="Cancel" OnClick="Button1_Click" /&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
    &lt;%--&lt;asp:Button ID="btn1" runat="server" Text="Ok" onclick="btn1_Click" /&gt;--%&gt;
&lt;/div&gt;
</code></pre>

<p>On cancel click I am getting the alert I needed, the same is not getting when I close the tab where I went wrong can some one help me</p>

<p>This kind of scenario i need with my custom alert</p>

<p><a href="http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html" rel="nofollow">http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html</a></p>

<p><img src="http://i.stack.imgur.com/7vC3p.jpg" alt="enter image description here"></p>
