Asp classic 在web浏览器中捕获选项卡关闭事件?

Asp classic 在web浏览器中捕获选项卡关闭事件?,asp-classic,Asp Classic,有没有办法知道用户是否关闭web浏览器中的选项卡?特别是IE7,也包括FireFox和其他浏览器。如果包含我们网站的当前选项卡关闭,我希望能够通过asp代码处理这种情况。是否执行文档卸载?如果附加“”事件,则不执行此操作。它可以在浏览器/选项卡关闭之前执行代码。如果您需要知道服务器端的页面何时关闭,最好是定期从页面ping服务器(例如,通过XMLHttpRequest)。当ping停止时,页面关闭。如果浏览器崩溃、终止或计算机关闭,这也会起作用。正如Eran Galperin所建议的,在卸载之前

有没有办法知道用户是否关闭web浏览器中的选项卡?特别是IE7,也包括FireFox和其他浏览器。如果包含我们网站的当前选项卡关闭,我希望能够通过asp代码处理这种情况。

是否执行文档卸载?如果附加“”事件,则不执行此操作。它可以在浏览器/选项卡关闭之前执行代码。

如果您需要知道服务器端的页面何时关闭,最好是定期从页面ping服务器(例如,通过
XMLHttpRequest
)。当ping停止时,页面关闭。如果浏览器崩溃、终止或计算机关闭,这也会起作用。

正如Eran Galperin所建议的,在卸载之前使用
on
。特别是,返回一个字符串,该字符串将包含在确认对话框中,该对话框将允许用户选择是否离开页面。每个浏览器在返回的字符串前后都包含一些文本,因此您应该在不同的浏览器中进行测试,以查看用户将看到什么。

我确信OP是从网页本身的上下文中提出的,但是对于遇到此问题的任何Firefox插件开发人员,您都可以使用
TabClose
事件

onbeforeunload也会在以下事件中被调用-

* Close the current browser window.
* Navigate to another location by entering a new address or selecting a Favorite.
* Click the Back, Forward, Refresh, or Home button.
* Click an anchor that refers the browser to another Web page.
* Invoke the anchor.click method.
* Invoke the document.write method.
* Invoke the document.open method.
* Invoke the document.close method.
* Invoke the window.close method.
* Invoke the window.open method, providing the possible value _self for the window name.
* Invoke the window.navigate or NavigateAndFind method.
* Invoke the location.replace method.
* Invoke the location.reload method.
* Specify a new value for the location.href property.
* Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.

因此,如果您试图在用户关闭选项卡或浏览器窗口时注销用户,那么每次用户单击链接或提交页面时,您都会将其注销

Santosh是对的,除了单击选项卡/浏览器的“关闭”按钮外,还会有更多操作触发此事件。我创建了一个小技巧,通过在DocumentReady上添加以下函数来防止触发onbeforeunload操作

       $(function () {     
        window.onbeforeunload = OnBeforeUnload;
            $(window).data('beforeunload', window.onbeforeunload);
            $('body').delegate('a', 'hover', function (event) {
                if (event.type === 'mouseenter' || event.type === "mouseover")
                    window.onbeforeunload = null;
                else
                    window.onbeforeunload = $(window).data('beforeunload');
            });
        });
此外,在触发Santosh提到的任何事件之前,您需要运行以下命令行:

window.onbeforeunload = null;
如果您不希望触发该事件

这是最后一段代码:

    function OnBeforeUnload(oEvent) {   
            // return a string to show the warning message (not every browser will use this string in the dialog)
            // or run the code you need when the user closes your page  
        return "Are you sure you want to close this window?";       
    }

document.unload是客户端的,因此您必须更进一步才能在服务器端(在asp代码中)处理它,并可能从document unload事件处理程序发出AJAX请求?异步调用可以工作吗?我是说。。。选项卡正在被终止,当然最好是同步并以某种方式锁定它。您不需要同步请求,因为页面不关心响应(事实上,在返回响应时它可能会关闭)。谢谢大家。我正在将这些详细信息传递给我们的asp开发人员,并将根据她的发现对答案进行投票。请注意,onbeforeunload在Opera中不起作用。onunload在Opera中工作,但仅当通过单击链接离开页面时才起作用。如果关闭选项卡或重新加载页面,Opera中不会触发onunload。在开始使用onbeforeunload事件之前,请确保您了解它的功能。阅读桑托什的答案和我的答案。这将节省您很多时间,因为如果您只是附加一个像Eran建议这样的onbeforeunload事件,您将看到此事件的触发次数比您预期的要多。