是否有任何方法知道,如果用户关闭一个标签,在一个网络浏览器?具体地说IE7,但也FireFox和其他人。我想要能够处理这种情况下,从我们的asp代码,如果目前的选项包含我们的网站将关闭。

有帮助吗?

解决方案

附加“ onbeforeunload " ;事件。它可以在浏览器/标签关闭之前执行代码。

其他提示

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.

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

如果你的话,document.unload不会这样做吗?

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

正如Eran Galperin建议的那样,使用 onbeforeunload 。特别是,返回一个字符串,该字符串将包含在一个确认对话框中,该对话框允许用户选择是否离开页面。每个浏览器在您返回的字符串之前和之后都包含一些文本,因此您应该在不同的浏览器中测试以查看用户将被呈现的内容。

我肯定运问从上下文的网页,但为任何火狐-加-开发人员遇到这个问题,可以使用 TabClose 事件。 https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed

Santosh是正确的,这一活动将触发更多的行动不仅仅是点击闭按钮你tab/浏览器。我已经创建了一个小小的黑客,以防止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');
            });
        });

此外,在你的触发的任何事件提到桑托斯,你需要运行这一行:

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?";       
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top