最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Detect Close Browser in ASP.NET - Stack Overflow

programmeradmin0浏览0评论

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a 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 is closing the browser tab. I used the following code in the new aspx page:

<script type="text/javascript">
    $(window).bind("beforeunload", function () {
        $(window).unbind("beforeunload");
        return confirm("Do you really want to close?")
    })
</script>

the problem is that if i press also other buttons than the browse closeTab the method works. i would like to know how can i avoid it.

thanx in advance.

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a 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 is closing the browser tab. I used the following code in the new aspx page:

<script type="text/javascript">
    $(window).bind("beforeunload", function () {
        $(window).unbind("beforeunload");
        return confirm("Do you really want to close?")
    })
</script>

the problem is that if i press also other buttons than the browse closeTab the method works. i would like to know how can i avoid it.

thanx in advance.

Share Improve this question asked May 17, 2013 at 8:52 H_79H_79 2174 gold badges7 silver badges13 bronze badges 1
  • 1 Please check this thread stackoverflow./questions/1997956/… – Dmitri Tsoy Commented May 17, 2013 at 14:53
Add a ment  | 

1 Answer 1

Reset to default 3

Yes, you should use something like this:

<script language="JavaScript" type="text/javascript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
    return "You are about to exit the system before freezing your declaration! If you leave now and never return to freeze your declaration; then they will not go into effect and you may lose tax deduction, Are you sure you want to leave now?";
    }
    $(function() {
        $("a").click(function() {
            window.onbeforeunload = null;
        });
        $("input").click(function() {
            window.onbeforeunload = null;
        });
    });
</script>

So this message is not showing everytime you refresh the page

Link here to the source

发布评论

评论列表(0)

  1. 暂无评论