INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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: 7 Jul 00

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:
<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>
<A HREF="javascript:popNewWindow();">Open up a new window!"</A>
</BODY></HTML>


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


You can also change form elements and other data by referring to the first page as window.opener 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 window.opener.location.href instead of window.opener.location.pathname. Best of luck, and I hope that helps.

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close