Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I open a new window in JavaScript and then change the opener's URL, closing the new window?

Opening and closing a window

How do I open a new window in JavaScript and then change the opener's URL, closing the new window?

by  carpeliam  Posted    (Edited  )
Sometimes you want to do some user interaction without cluttering up your main window. One easy way to do this is by opening up a new window.

Here's how you can do it.

First, you'll have a main page from which you'll open the new, smaller window.
It could have a form somewhat like this:
[tt]<HTML>
<HEAD>
<TITLE>Your Title</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popNewWindow() {
window.open("yourpage.html", "popup",
"width=400,height=389,scrollbars=no,menubar=no");
}
//-->
</SCRIPT>
</HEAD>
<BODY>
Open up a new window!"
</BODY></HTML>[/tt]

Then, in an HTML page called "yourpage.html", you can have something like this:
[tt]<HTML>
<HEAD>
<TITLE>Your Pop-up Window</TITLE>
<SCRIPT language="Javascript">
<!--
function closeAndMove() {
window.opener.location.pathname = "/anotherpage.html";
window.close();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
Close this window and change location of opening page
</BODY></HTML>[/tt]

You can also change form elements and other data by referring to the first page as [tt]window.opener[/tt] in the DOM. If you wanted to open a new page in the main window that didn't happen to have the same base URL, then you'd use [tt]window.opener.location.href[/tt] instead of [tt]window.opener.location.pathname[/tt]. Best of luck, and I hope that helps.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top