How To Get Child Window Object In Javascript

In Javascript, there might be times you need to access the child window object. This could be because you want to manipulate or retrieve data from the child window object. In this tutorial, we will show you how to get the child window object in Javascript.

Steps:

1. Open a new child window

To get the child window object, we first need to open a new child window. We can do this using the window.open() method. This method takes parameters for the URL of the page to open and the name for the new window. Here’s an example:

This code will open a new child window with the URL of https://www.example.com and the name of ‘childWindow’.

2. Access the child window object

Now that we have opened the child window, we can access the child window object using the window.frames property.

This property returns an array-like object that contains all of the child window objects. We can use the window.frames.length property to get the number of child window objects and the index of the child window object we want to access. Here’s an example:

This code will get the first child window object and assign it to the childWindowObject variable.

3. Manipulate or retrieve data from the child window object

Now that we have the child window object, we can manipulate or retrieve data from it using standard Javascript methods. For example, we can access the child window’s document object to get or set its HTML content. Here’s an example:

This code will get the child window’s document object and assign it to the childWindowDocument variable.

We then use the outerHTML property to get the HTML content of the child window and assign it to the childWindowHtml variable. We then output the HTML content to the console using the console.log() method.

Conclusion:

In this tutorial, we showed you how to get the child window object in Javascript. We used the window.open() method to open a new child window, the window.frames property to access the child window object, and standard Javascript methods to manipulate or retrieve data from the child window object.

Full code: