Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

javascript - How to detect the window(new tab) close event?

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)

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...'); 
}

and

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.

Update

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>

How to acheive this in FireFox and other Browser.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is afaik never been a cross browser script for this. The solution is to NOT rely on undocumented and changeable features of a specific browser to detect something that is important.

Since you have a CHILD page, you can set up a test in the parent (opener) that at intervals test the childWindowHandle.closed property and acts on that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...