如何检测窗口(新标签)关闭事件? [英] How to detect the window(new tab) close event?

查看:646
本文介绍了如何检测窗口(新标签)关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父页面和子页面。子页面在新标签页中打开

I have one parent page and child page. the child page opened in new tab

我想关闭子标签时显示一条警告消息(子页面正在关闭) 。$
关闭标签时如何显示关闭的杂物? (不刷新时间)

I want to show one alert message (The child page is closing), when i close the child tab.
How to show the closing messgae, when close the tab? (Not refreshing time)

我使用 onunload onbeforeunload

两种方法当页面刷新和制表符关闭时,也会调用它们。

I used onunload, and onbeforeunload.
Two methods are also called, when the page refresh and tab closing.

window.onunload = function doUnload(e)
{
  alert('Child window is closing...'); 
}

window.onbeforeunload = function doUnload(e)
{
  alert('Child window is closing...'); 
}

我必须显示警告信息,只有关闭标签在浏览器中。

I must show the alert message, only close the tab in browser.

帮助我。
提前致谢。

Help me. Thanks in advance.

更新

我使用以下内容脚本。它在IE中工作。但是没有在FireFox中工作

I use the following script. Its worked In IE. But not worked in FireFox

  <script language="javascript" type="text/javascript">          

  window.onbeforeunload = function()
  {   
      if ((window.event.clientX < 0) || (window.event.clientY < 0) || (window.event.clientX < -80)) 
     {            
          alert("Child window is closing...");  
     }   
 };   

</script>

如何在FireFox和其他浏览器中实现此功能。

How to acheive this in FireFox and other Browser.

推荐答案

脚本是否来自 http://chrismckee.co.uk/good-sir-before-you-unload-crossbrowser-javascript-headaches/ 有效吗?

假设你只是想激发beforeunload事件跨浏览器,这几乎做它(不包括戏曲)

Assuming your just trying to fire beforeunload event crossbrowser, this pretty much does it ( excluding opera )

try{
    // http://www.opera.com/support/kb/view/827/
    opera.setOverrideHistoryNavigationMode('compatible');
    history.navigationMode = 'compatible';
}catch(e){}

//Our Where The F' Are You Going Message
function ReturnMessage()
{
    return "WTF!!!";
}

//UnBind Function
function UnBindWindow()
{
    window.onbeforeunload = null;
    return true;
}

//Bind Links we dont want to affect
document.getElementById('homebtn').onclick = UnBindWindow;
document.getElementById('googlebtn').onclick = UnBindWindow;

//Bind Exit Message Dialogue
window.onbeforeunload = ReturnMessage;

这篇关于如何检测窗口(新标签)关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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