Click here to Skip to main content
Click here to Skip to main content

Accessing parent window from child window or vice versa using JavaScript

, 19 Apr 2008
Rate this:
Please Sign up or sign in to vote.
This article explains basic options available to access the data present in a parent browser window from a child browser window or vice versa.

Introduction

This article explains basic options available to access the data present in a parent browser window from a child browser window or  vice versa. By telling the word Parent I mean the browser window that opens another browser window, the new window is what I call Child. At times when you are developing a web application you could face a situation, where there is a need to access the HTML forms and controls present in a parent window from a popup window or something similar, this article provides few solutions for accomplishing this. I am considering only IE and Firefox for this article, these options may work in other browsers also. I am going to mention the basic options, which someone can use immediately in their work, instead of analyzing all the options/workarounds available for a specific need.

Topics discussed:

 

Using window.opener object to access the parent browser window from a child window: (compatible with IE & Firefox)

The window.opener object can be used to access the HTML elements present in the parent browser window from the child window(popup or similar).

Let us consider two HTML files:

openwindow.htm – this has a text box and a button, clicking the button opens the target.htm file in a new browser window
target.htm – this has code to change the value of the text box present in the parent window(openwindow.htm) that opens this file

openwindow.htm:


<html>

<script language="javascript">

function openwindow()

{

window.open("target.htm","_blank","height=200,width=400,

status=yes,toolbar=no,menubar=no,location=no")

}

</script>

<body>

<form name=frm>

<input id=text1 type=text>

<input type=button onclick="javascript:openwindow()" value="Open window..">

</form>

</body>

</html>

If you are not familiar with window.open() method then you would be wondering what is "height=200, width=400,status=yes,toolbar=no,menubar=no,location=no", don’t worry, it just specifies the size and appearance of the new window, this line of code can be changed to window.open("target.htm","_blank") just see the difference in output if you change.

Note that if the parameter ”_blank” is not provided then the functionality will differ between IE and Firefox, in firefox a new tab will be opened instead of a new window. Just use window.open("target.htm") and see the difference yourself. For more info about window.open() method and its parameters, see http://msdn2.microsoft.com/en-us/library/ms536651(VS.85).aspx. Also note, in <input id=text1 type=text>, the id attribute is necessary, then only the code of target.htm(shown below) will execute in Firefox.

target.htm:


<html>

<script language="javascript">

function changeparent()

{

window.opener.document.getElementById('text1').value="Value changed.."

}

</script>

<body>

<form>

<input type=button onclick="javascript:changeparent()" value="Change opener's textbox's value..">

</form>

</body>

</html>

Hope you can get what is going on in the above code, in the new window(target.htm) we are using the window.opener object to access the text box present in the parent window(openwindow.htm). You just need to prefix “window.opener.” and write the same code that you will write in the parent window’s HTML page to access its elements.

 

Using window.showModalDialog() to send data to parent browser window from a child window: (only IE, wont work in Firefox)

If you want to open a modal dialog and access the parent window’s elements in it, then this method can be used, but this will work only in IE. (If you are new to the term modal dialog, see http://en.wikipedia.org/ wiki/Modal_window) Here we will be calling the window.showModalDialog() method to open the child window and assign the return value of this method to a js variable, the return value for the window.showModalDialog() method is provided in the child window by using the window.returnValue property, as shown in the below example.

Here I am going to show two files similar to the example shown for window.opener object.

showmodal.htm – this has a text box and a button, clicking the button opens the modaltarget.htm file in a new browser window
modaltarget.htm – this has code to change the value of the text box present in the window that opens this file

showmodal.htm:


<html>

<script language="javascript">

function openwindow()

{

retval=window.showModalDialog("modaltarget.htm")

document.getElementById('text1').value=retval

}

</script>

<body>

<form name=frm>

<input name=text1 type=text>

<input type=button onclick="javascript:openwindow()" value="Open window..">

</form>

</body>

</html>

modaltarget.htm:


<html>

<head>

<script language="javascript">

function changeparent()

{

window.returnValue="Value changed.."

window.close()

}

</script>

</head>

<body>

<form>

<input type=button onclick="javascript:changeparent()" value="Change main window’s textbox value..">

</form>

</body>

</html>

As you can see, in showmodal.htm, the retval variable is assigned with the result of the window.showModalDialog() method call, and in the next line the text box’s value is set, but this line of code will execute when the modal window gets closed, in all modal window calls the next line executes only after the modal dialog closes, this is the reason I close the window after I set the returnValue in modaltarget.htm. If you remove the window.close(), then the second line in changeparent() function will not execute until you manually close the window. The showModalDialog() is not a w3c standard and not supported by Firefox.

There is a method called showModelessDialog() if you are interested in displaying a modeless dialog you can use this. As my aim is to provide basic options and quick solutions to readers; and as this modeless functionality can be done using window.open() I am not explaining this.

 

Accessing child browser windows from parent windows: (not working in Firefox)

We can access the child window elements by using the window handle that will be returned by the window.open() method, as shown below:

winobj=window.open(..)
...
winobj.document.getElementById("elementname").value=""

The winobj is actually a window object instance of the newly created window.

 

Calling JavaScript functions in parent/client windows:

Calling JavaScript functions present in the parent window from child window: (IE & Firefox)

You can actually call a JavaScript function that is present in the parent window by using the below syntax:

window.opener.functioname()

Calling JavaScript functions present in the child window from parent window: (IE & Firefox)

To call JavaScript functions present in child window, use the window handle as shown below:

winobj=window.open(..)
...
winobj.functionname()

We can even have a function with arguments and use it as a workaround to share data between parent and child browser windows at some scenarios as shown below:

window.opener.functioname(arg1,arg2,..)
- to send data to the parent window from child window

winobj.functioname(arg1,arg2,..)
- to send data to the child window from parent window using the window handle got from window.open()


The options mentioned in this article can be used to access multiple child windows and also multiple child windows can access a single parent window.


Conclusion:

I have explained methods to access and share data between child browser windows and parent windows. There may be many such options or workarounds available, if so please leave a comment by specify your method.

 

 

License

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

Share

About the Author

Ashoka RKT
Technical Lead
India India
I am Ashoka (R.K.T.Ashoka), I am a software professional who is passionate about designing and developing software applications. I blog at www.codesmiles.com, I will be writing about .Net & programming.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionParent window refresh once child window close PinmemberMember 1093511413-Jul-14 22:13 
GeneralRefresh parent page on closing modal child window/popup Pingroupelizas23-Mar-10 2:02 
AnswershowModelessDialog [modified] Pinmembermadgardener4-Sep-08 21:38 
Questionhello sir can u help me i have a problem in Html and java script? Pinmemberrohit wadhwa15-May-08 19:47 
Questionhello sir can u help me i have a problem in Html and java script? Pinmemberrohit wadhwa15-May-08 19:44 
GeneralExcellent Pinmemberfunkyasl1-May-08 2:01 

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 | Mobile
Web02 | 2.8.140827.1 | Last Updated 19 Apr 2008
Article Copyright 2008 by Ashoka RKT
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid