繁体   English   中英

如何捕获用户意外离开ASP.Net页的时间

[英]How to capture when a user is leaving ASP.Net page unexpectedly

我需要在用户意外离开我的ASP.Net页时提示他们,并提示他们是否确定要离开。 回发信息或单击“保存”按钮不应触发警告。 有很多关于此的文章,但是我对此是陌生的,似乎使我的话不对。

推荐的方法似乎是使用window.onbeforeunload事件,但对我而言表现异常。 当页面加载时(而不是页面卸载时)触发该事件。

<script language="JavaScript" type="text/javascript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
    }
</script>

如果我使用JQuery实现,则会在页面卸载时触发,但问题是它在执行后面的代码之前触发。 因此,我无法在客户端上设置一个变量,说这次不要触发该事件,因为它是回发或保存的。

$(window).bind('beforeunload', function () {
            return 'Are you sure you want to leave?';
        }); 

当我知道我犯了基本的错误/误解时,有人能指出我正确的方向吗?

编辑:

所以我快到了:

var提示= true;

$('a').live('click', function () {

    //if click does not require a prompt set to false
    prompt = false;
});

$(window).bind("beforeunload", function () {

    if (prompt) {

        //reset our prompt variable
        prompt = false;

        //prompt
        return true;
    }
})

除非问题出在上述代码中,否则我需要能够区分点击次数,但我仍无法弄清楚,即我在这里缺少条件“ //如果点击不需要将提示设置为false ”。

有任何想法吗?

谢谢迈克尔

您可以尝试使用以下方法:

$(window).unload(function(){
  alert('Message');
});

如果人们有兴趣,这是解决我的问题的回旋解决方案。 如何判断ASP中的页面卸载是否为回发

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM