views:

44

answers:

3

Hi, 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.

A: 

There is an example in Mozilla Devloper site which basically says check for browser type and use the below check accordingly.Hope this helps.

GustlyWind
A: 

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.

mplungjan
A: 

Hi,

I can detect the tab close event in IE and FireFox.

Then i want to detect newtab close event in other browser (Not refreshing child page)

Main.jsp

<body>
<a href="child.jsp" target="_blank">New Tab</a>
</body>

child.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; 

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <script type="text/javascript">

            function doUnload()
            {                    
                if(navigator.userAgent.toLowerCase().indexOf("firefox") > 0)
                {
                    var winWidth = document.body.clientWidth;
                    var winHeight = document.body.clientHeight;                   
                    if ((winWidth <= 0) || (winHeight <= 0) || (winWidth <= -80))
                    {
                      alert("Child Tab is closing in FF");                        
                    }
                }

                if(navigator.appName == "Microsoft Internet Explorer")
                {
                    if ((window.event.clientX < 0) || (window.event.clientY < 0) || (window.event.clientX < -80))
                    {
                        alert("child tab is closing in IE");                        
                    }
                }                                       
            }

        </script>
</head>
<body onunload="doUnload()">   

    <h1>This is Chile Page.</h1>

</body>
</html>

Similarly, I want to show the message "child tab is closing" when i close the child tab in other browser. like Opera, Google Chrome etc.

Help me about this. Thanks in advance.

EswaraMoorthyNEC
here this answer not update my original Post. Sorry for my inconvenience post.
EswaraMoorthyNEC