Is there any way of knowing if the user closes a tab in a web browser? Specifically IE7, but also FireFox and others as well. I would like to be able to handle this situation from our asp code if the current tab containing our web site closes.
有没有办法知道用户是否关闭了Web浏览器中的选项卡?特别是IE7,还有FireFox等。如果包含我们网站的当前选项卡关闭,我希望能够从我们的asp代码处理这种情况。
7 个解决方案
#1
2
Attach an "onbeforeunload" event. It can execute code just before the browser/tab closes.
附上“onbeforeunload”事件。它可以在浏览器/选项卡关闭之前执行代码。
#2
6
onbeforeunload also gets called on the following events -
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.
So, if you are trying to log out the user if they close the tab or browser window, you would end up logging them out everytime they click a link or submit the page.
因此,如果您在关闭选项卡或浏览器窗口时尝试注销用户,则每次单击链接或提交页面时,最终都会将其注销。
#3
1
Does document.unload not do it if you?
如果你的话,document.unload不会这样做吗?
#4
0
If you need to know when the page is closed at the server side, your best bet is to ping the server periodically from the page (via XMLHttpRequest
, for example). When pinging stops, the page is closed. This will also work if the browser crashed, was terminated or the computer was turned off.
如果您需要知道服务器端何时关闭页面,最好的办法是定期从页面ping服务器(例如,通过XMLHttpRequest)。当ping停止时,页面将关闭。如果浏览器崩溃,终止或计算机已关闭,这也将起作用。
#5
0
As Eran Galperin suggested, use onbeforeunload
. Particularly, return a string, and that string will be included in a confirmation dialog which will allow the user to choose whether or not to leave the page. Each browser includes some text before and after the string you return, so you should test in different browsers to see what a user would be presented with.
正如Eran Galperin建议的那样,使用onbeforeunload。特别是,返回一个字符串,该字符串将包含在确认对话框中,允许用户选择是否离开页面。每个浏览器都包含您返回的字符串之前和之后的一些文本,因此您应该在不同的浏览器中测试以查看用户将被呈现的内容。
#6
0
I'm sure the OP is asking from the context of the web page itself, but for any Firefox-addon-developers who come across this question, you can use the TabClose
event. https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed
我确信OP正在从网页本身的上下文中询问,但对于遇到此问题的任何Firefox-addon开发人员,您可以使用TabClose事件。 https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed
#7
0
Santosh is right, this event will be triggered on many more actions than just clicking the close button of your tab/browser. I've created a little hack to prevent the onbeforeunload action to be triggered by adding the following function on document ready.
Santosh是对的,除了单击选项卡/浏览器的关闭按钮之外,此事件将在更多操作上触发。我已经创建了一个小小的hack来防止通过在文档就绪上添加以下函数来触发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');
});
});
Also, before you trigger any of the events mentioned by Santosh, you need to run this line:
此外,在触发Santosh提到的任何事件之前,您需要运行以下行:
window.onbeforeunload = null;
If you do not want the event to be triggered.
如果您不希望触发事件。
And here's the final code piece:
这是最终的代码片段:
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?";
}
#1
2
Attach an "onbeforeunload" event. It can execute code just before the browser/tab closes.
附上“onbeforeunload”事件。它可以在浏览器/选项卡关闭之前执行代码。
#2
6
onbeforeunload also gets called on the following events -
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.
So, if you are trying to log out the user if they close the tab or browser window, you would end up logging them out everytime they click a link or submit the page.
因此,如果您在关闭选项卡或浏览器窗口时尝试注销用户,则每次单击链接或提交页面时,最终都会将其注销。
#3
1
Does document.unload not do it if you?
如果你的话,document.unload不会这样做吗?
#4
0
If you need to know when the page is closed at the server side, your best bet is to ping the server periodically from the page (via XMLHttpRequest
, for example). When pinging stops, the page is closed. This will also work if the browser crashed, was terminated or the computer was turned off.
如果您需要知道服务器端何时关闭页面,最好的办法是定期从页面ping服务器(例如,通过XMLHttpRequest)。当ping停止时,页面将关闭。如果浏览器崩溃,终止或计算机已关闭,这也将起作用。
#5
0
As Eran Galperin suggested, use onbeforeunload
. Particularly, return a string, and that string will be included in a confirmation dialog which will allow the user to choose whether or not to leave the page. Each browser includes some text before and after the string you return, so you should test in different browsers to see what a user would be presented with.
正如Eran Galperin建议的那样,使用onbeforeunload。特别是,返回一个字符串,该字符串将包含在确认对话框中,允许用户选择是否离开页面。每个浏览器都包含您返回的字符串之前和之后的一些文本,因此您应该在不同的浏览器中测试以查看用户将被呈现的内容。
#6
0
I'm sure the OP is asking from the context of the web page itself, but for any Firefox-addon-developers who come across this question, you can use the TabClose
event. https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed
我确信OP正在从网页本身的上下文中询问,但对于遇到此问题的任何Firefox-addon开发人员,您可以使用TabClose事件。 https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed
#7
0
Santosh is right, this event will be triggered on many more actions than just clicking the close button of your tab/browser. I've created a little hack to prevent the onbeforeunload action to be triggered by adding the following function on document ready.
Santosh是对的,除了单击选项卡/浏览器的关闭按钮之外,此事件将在更多操作上触发。我已经创建了一个小小的hack来防止通过在文档就绪上添加以下函数来触发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');
});
});
Also, before you trigger any of the events mentioned by Santosh, you need to run this line:
此外,在触发Santosh提到的任何事件之前,您需要运行以下行:
window.onbeforeunload = null;
If you do not want the event to be triggered.
如果您不希望触发事件。
And here's the final code piece:
这是最终的代码片段:
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?";
}