Last updated on March 29th, 2022 at 07:21 am

In this tutorial we are going to see how to open child window from a browser parent and close the same child window using Javascript.

We are assigning a variable named childwin to window.open function and then just use childwin.close() to eventually close the child browser window that got opened from a parent.

<title>Example For Child and Parent Window Operations</title>
<center>
<script language="JavaScript">
function closeDep()
	{
if (typeof childwin === 'undefined')
{
			alert("No Child Window Open")
		}
		else
		{
			alert("Going to close child window")
	childwin.close()
		}
delete childwin
}
</script>
<body >
<div>
<a href="" onclick="childwin= window.open('https://www.mistonline.in/wp', 'win', 'height=650,width=700'); main='getme';return false">
First CLICK ME ----> Mistonline tutorial world</a></div><p>Now after clicking the above link click this button please
<hr><input type="button" value="Child Window Close" onClick="closeDep();"><br><br>
<a href="http://mistonline.in/wp/how-to-close-a-child-window-from-a-parent-window-using-javascript/">GO BACK TO THE TUTORIAL</a>

Initial check typeof childwin === 'undefined' , we verify if childwin variable is set. ie If a child window is opened.

As you can see above I am adding delete childwin, this is done to unset the variable childwin . Special logic to make sure that after a user click Child Window Close button for the first time to close a window and if they attempt to click on that button again then the script will go back to the initial check where the variable childwin was not set and alert the user with a message No Child Window Open

DEMO

3 thoughts on “How to open child window from browser parent window using javascript”

Leave a Reply

Your email address will not be published. Required fields are marked *