Close Parent Browser Window

We all at some point in time in our web development projects had to close browser windows automatically.

DevPro Staff

August 10, 2004

3 Min Read
ITPro Today logo in a gray background | ITPro Today

We all at some point in time in our web development projects had to close browser windows automatically.

 

We can easily close the pop-up windows that we have created. Here we will see how to close the parent window that was opened by the user.

 

function CloseWin()

{

            window.close();

}

 

The above javascript function warns the user that the parent window is being closed and the user has the option to cancel the closure of the window.

 

We can close the window without the warning being flashed to the user by using the opener property of the window. The new code is shown below.

 

function CloseWin()

{

window.opener = top ;

            window.close();

}

 

Copy the above code in notepad and save it as an html file. Double clicking on the file opens it in the browser and closes it immediately.

 

We can also open a new window by adding another line of code.

 

function CloseWin()

{

window.open("http://www.msd2d.com","newwin")window.opener = top ;

            window.close();

}

 

This opens a new window and closes the parent window.

 

All the above functions can be called in the onload event of the body tag in the page.

  

Happy Coding!!

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like