赶上关闭浏览器的onunload与功能的使用ASP.NET母版页 [英] catch close browser with onunload function using master page in ASP.NET

查看:467
本文介绍了赶上关闭浏览器的onunload与功能的使用ASP.NET母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版页网站。我要赶试图关闭浏览器/标签的用户。当我尝试在body标签使用onunload的,它触发不仅当我尝试关闭浏览器,而且当我浏览到另一个网页。

I have a website with master page. I want to catch a user trying to close the browser/tab. When I try using onunload in the body tag, it fires not only when I try closing the browser, but also when I navigate to another page.

不知道如何搭上只关闭浏览器的事件?

Any idea how to only catch the event of closing the browser?

推荐答案

您不能区分导航到另一个页面关闭浏览器。在两种情况下,当前页面被卸载。

You can't distinguish closing the browser from navigating to another page. In both cases the current page is unloaded.

更新:也许你可以处理某些情况下一些jQuery,即只要点击一个链接,设置一些标志能够从关闭窗口或进入一个新的URL区别:

update: maybe you can handle some cases with some jquery, i.e. whenever a link is clicked, set some flag to be able to distinguish it from closing the window or entering a new URL:

<body onunload="checkForClose()">

...

<script>
var _isNavigation = false;
$(document).ready(function () {
    // whenever a link is clicked set _isNavigation to true
    $('a').click(function () {
        _isNavigation = true;
    });
});

function checkForClose() {
    // show an alert if _isNavigation is not set
    if (!_isNavigation) alert("closing the browser (maybe)");
}
</script>

这篇关于赶上关闭浏览器的onunload与功能的使用ASP.NET母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆