Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
HI,

i have an application in one web page(A) have 1 button on click of that button one page(B) is opened i.e new in new window so when parent page A is closed then page B should close.when we close browser of A then B should also close.
please help me how it should be done.

i tried with the below code but it is not working

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

function ClosePopUp() {
window.close('B.aspx');
window.self.close();
}

</script>
<body önunload="ClosePopUp()">
</body>


here ClosePopUp() is calling but page B is not closing why?any other solution please help

Thanks
D srinivas
Posted
Updated 14-Mar-13 9:10am
v2
Comments
Sergey Alexandrovich Kryukov 14-Mar-13 15:04pm    
And your question is..? Why doing so?
—SA
bbirajdar 14-Mar-13 15:04pm    
not possible
Sergey Alexandrovich Kryukov 14-Mar-13 15:14pm    
Why not? I think I answered. You can capture closing of the page, and it can close another, as you can remember the child windows in the script of the parent after creation.
If one wanted a "child" window to close the "parent", it would be a problem, but when a parent closes child, it'll work fine...
I basically answered, please see.
—SA

1 solution

The request is quite questionable. Why doing so?

However, these pages will give you the idea:
http://www.w3schools.com/jsref/obj_window.asp[^],
http://www.w3schools.com/jsref/met_win_close.asp[^],
http://stackoverflow.com/questions/6622461/how-to-capture-browser-close-event-in-javascript[^].

Does it make your problem clear?

[EDIT]

As we had a little argument with aspnet_regiis, this is the code I've just tested, so it proves my point:
XML
<html>
<head>
   <title>Child window and window.onbeforeunload demo</title>
   <script language="Javascript"><!--
      var newWin;
      function Show() {
         newWin = window.open(
            "",
            "toolbar=no,location=no"
         );
         newWin.name = "Child window";
      }
      function CloseIt() {
         alert("Closing a child window"); newWin.close();
      }
    --></script>
</head>

<body>

<script language="Javascript"><!--
    window.onbeforeunload = function(e) {
        CloseIt();
    };
--></script>

<button accesskey="S" onClick="Show()"><u>S</u>how</button></br>

</body>
</html>


By the way, despite of my initial comment, closing a "parent" window from a child one is also possible, due to the property opener:
http://www.w3schools.com/jsref/prop_win_opener.asp[^].

—SA
 
Share this answer
 
v11
Comments
bbirajdar 14-Mar-13 15:22pm    
Dear SA...With due respect to you, I want to put forth that the two browser instances are independent of each other and one does not know about each other... We cannot close one browser instance from another.

I would be more then happy if you can prove me wrong. I still insist that one browser instance cannot be closed from another and there is no such thing like a parent or a child window. Its a logical parent and child window. technically a parent window does not know about child window ( other than the window properties used while opening the child window)....
Sergey Alexandrovich Kryukov 14-Mar-13 15:36pm    
I see your point, but... I think that they become dependent when you store a window reference in the parent page... A while ago, I did something very similar (the code is on my Web site, a photo album; you can see it). I did not close the child window (which would look stupid to me, but I replaced its content on some anchor click in the parent window. You should agree that if I can do it, it means that one page actually keeps the reference to the window of another page and can control it, in particular, close it.

Do you still need to see such code..? Or you can try by yourself.

I tested it with different (old) browsers, it works now; and even when people complained about something, nobody observed that my mechanism failed...
—SA
Sergey Alexandrovich Kryukov 14-Mar-13 16:12pm    
OK, I got the fully-working demo code to show you. Please try.
Click on the button "Show" first, than close the first ("parent") window. It should close the "child" one.

Will you change your opinion now?
—SA
bbirajdar 15-Mar-13 0:16am    
Surely.. I will try it and will let you know ..And thanks for the complete working code...
bbirajdar 15-Mar-13 2:59am    
Amazing.. It works....Thank you very much for correcting me... I wish I could have voted you +10

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900