Click here to Skip to main content
15,867,330 members
Articles / Web Development / HTML
Article

Parent - Child relationship of Windows in JavaScript

Rate me:
Please Sign up or sign in to vote.
3.36/5 (8 votes)
7 Feb 20052 min read 136.7K   459   21   12
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:

JavaScript
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:

JavaScript
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


Written By
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.

Comments and Discussions

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

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

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