Click here to Skip to main content
11,565,619 members (41,739 online)
Click here to Skip to main content

Parent - Child relationship of Windows in JavaScript

, 7 Feb 2005 120.9K 432 20
Rate this:
Please Sign up or sign in to vote.
Controlling the parent window with child windows.

Introduction

In this article we are going to discuss something on the parent-child behavior of JavaScript.

Description

To understand more on the parent-child relationship let's take an example. I have a browser window and inside that I am clicking on a link. This link will open up a child window and inside that I have more links. Some link(s) that are available inside the child window will open the pages in new window(s). Now if this keeps on growing and from the first browser window (parent) we will end up with some 'n' number of child-subchild relationships. Question is that from the nth child window I would like to open one link inside the parent (first browser) window. How are you going to achieve this?

We will discuss the same in the following code block:

if(typeof(window.opener)!="undefined") 
{ 
   var x_win = window.self; 
   while(x_win!="undefined") 
   { 
      x_win = x_win.opener; 
      if(typeof(x_win.opener)=="undefined")                                   
      { 
         x_win.location.href="http://www.thecodeproject.com/"; 
         break; 
      } 
   } 
} 
else 
{ 
         window.location.href="http://www.thecodeproject.com/"; 
}

In the first line of code (typeof(window.opener)!="undefined") we are checking whether the window has a parent window? If yes, after this I am declaring a variable x_win and giving the reference of the current window by using window.self. As we wanted to achieve the functionality of opening a link of child or subchild window in parent, we go through a while(x_win!="undefined") loop which ensures that I land myself at the right place. Inside the while loop, I am assigning the variable x_win = x_win.opener to ensure that now the child or the subchild window points to its parent and at the moment I am at the parent and so, I have to check for a condition typeof(x_win.opener)=="undefined". Whenever I succeed in that condition it means that I am set to change the main page's layout or anything that you want to do.

Everything is simplified as of now while explaining about the parent-child relationship. Let me ask you one question: What will happen if I close the child 'x' window out of 'n' number of child windows? Well, you are right it will throw an exception which is necessary to handle. So, after changing the above code with some exception handling, it will look like this:

try
{
     if(typeof(window.opener)!="undefined") 
     { 
        var x_win = window.self; 
        while(x_win!="undefined") 
        { 
           x_win = x_win.opener; 
           if(typeof(x_win.opener)=="undefined")
            { 
              x_win.location.href="http://www.thecodeproject.com/"; 
              break; 
            } 
        } 
     } 
     else 
     { 
         window.location.href="http://www.thecodeproject.com/"; 
     }
}
catch(e)
{
    window.open("/"); // Here you can choose any operation, 
                      // which you want to do in case of a exception
}

Happy coding...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

AtulMalhotra
Web Developer
United States United States
Currently i am working as a Technical Architect Microsoft .Net Practices in Sogeti USA, NY. I am one of the winner of Microsoft Architect Contest 2004. I have 8 years of experience in the IT industry. Doing R&D on new technologies and reading about them is one of my big time interests.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
akhil.isc2-Jun-11 18:09
memberakhil.isc2-Jun-11 18:09 
QuestionParent and Child Window relation in Javascript Pin
Aarthiraaj30-Aug-07 7:20
memberAarthiraaj30-Aug-07 7:20 
GeneralPrevent parent window from receiving child cookie Pin
aarhma13-Jun-07 2:18
memberaarhma13-Jun-07 2:18 
GeneralRe: Prevent parent window from receiving child cookie Pin
AtulMalhotra13-Jun-07 4:36
memberAtulMalhotra13-Jun-07 4:36 
GeneralHi., Pin
trkannanbabu23-Nov-06 19:32
membertrkannanbabu23-Nov-06 19:32 
GeneralRefreshing Parent Window Pin
Majid Shahabfar1-Jan-06 23:39
memberMajid Shahabfar1-Jan-06 23:39 
GeneralRe: Refreshing Parent Window Pin
AtulMalhotra16-Feb-06 7:32
memberAtulMalhotra16-Feb-06 7:32 
GeneralRe: Refreshing Parent Window Pin
mat_odtu11-Apr-06 22:37
membermat_odtu11-Apr-06 22:37 
GeneralRe: Refreshing Parent Window Pin
AtulMalhotra12-Apr-06 1:57
memberAtulMalhotra12-Apr-06 1:57 
GeneralRe: Refreshing Parent Window Pin
mat_odtu12-Apr-06 9:48
membermat_odtu12-Apr-06 9:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 8 Feb 2005
Article Copyright 2004 by AtulMalhotra
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid