PDA

View Full Version : how do I open a new window from one page and close it from another?


CreekDawg99
03-17-2003, 05:07 PM
I have a web application in which a user fills out a form, clicks a "submit" button, and the results are returned to the screen.

This web application uses two different web pages for the form and the results.

What I am trying to accomplish is this: Once the user presses the submit button on the form, I open a new window that has a "Now searching..." text message. On the original page, there is some code that sets up the search and then loads another web page that displays the results. I want to be able to close the new window from the second page.

In the JavaScript function that is called when the "Submit" button is clicked from the first page, I use the following to open the new window:

hSearchWindow = window.open('searching.html', 'searchwindow', 'height=150, width=350, top=0, left=0, scrollbars=no');

How can I obtain on the second page the handle created (hSearchWindow) on the first page to close the new window? Or is there another way I should do this?

Any ideas would really be appreciated :)

kansel
03-17-2003, 07:55 PM
I use a pretty poor hack to accomplish this. On the second page (from which you want to close the popup) this should get a handle to the window and close it.


hSearchWindow = window.open('','searchwindow');
hSearchWindow.close();


Like I said, a complete hack. It just creates a new window with the same name (hence taking control of the already open popup) and then closes it.

CreekDawg99
03-17-2003, 08:26 PM
After I posted this message, I did some more research and quickly learned that the scope of the handle to the new window created on the first page would not last through to the second page as I had hoped. So, I was at a loss. However, I like the idea of your approach. It does exactly what I need.

Thanks very much for your help.

cheesebagpipe
03-17-2003, 10:26 PM
Like I said, a complete hack. It just creates a new window with the same name (hence taking control of the already open popup) and then closes it.Not really, and, not a hack at all - just good programming. The browser won't let you open two windows with the same name, since it uses these for targeting; so window.open() acts as a simple document (page) loader when it references an existent window. As always, it returns the window object involved (new or not), which gives you your handle.

jalarie
03-18-2003, 04:01 PM
kansel, thank you! I think you've just given me the answer to a somewhat related problem. :D

kansel
03-18-2003, 04:30 PM
On the site I first did this on, it was possible to visit page 2 without having the popup come up. With the code as it is, the page would open a window and immediately close it. In order to get around this, when the popup is open, the link to page 2 has a querystring on it like ?popup and then page two will only open and close the popup if that querystring is present.