Skip navigation

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!!

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish