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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting for existance of a popup window

Status
Not open for further replies.

TheDust

Programmer
Aug 12, 2002
217
US
I know how to reference a popup if it is created on the same page it is being referenced by. For instance:

Code:
var newWindow = window.open();
newWindow.close() // now referencing the new window from Javascript

However, is it possible in Javascript to close a popup window that was created on a completely different page? You can give the window a target name, but I am not sure how you can use that to have Javascript reference the popup window effectively. It seems like you have to create the popup on the same page or in the same script as when you reference it later on.

Any thoughts?
 
I am of the opinion that you can't access a popup window if it was opened from another domain. This is part of the "cross domain" security in browsers (and the subject of a lot of patches for browsers in the past).

So if we assume that you opened a popup in page1 and then navigated to page2 (all on the same domain) and you wanted to close that popup, then in theory you could still access the popup window.

If you are trying to do the latter... let us know... it may be worth trying.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Yes, I'm trying to do the latter. No cross-domain stuff. Has anyone seen this done before?
 
Note that the following is not my idea. I don't remember who originally posted it, but the answer I got to the same question here (a couple of years ago, I think) is this:

In your original window.open(...) statement, use the 2nd parameter to name the window. e.g., window.open(" "myWin");

Then, in the other page that you want to close that window, do this:

Code:
var closeWin = window.open("about:blank", "myWin");
closeWin.close();

The window that is opened will really just take over the window named myWin. Once you have control of that window (in the var myWin), you can close it.

'hope that helps. I usually test something like this before posting, but didn't do that in this case.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Brilliant! That works perfect. Thanks very much! The answer was so obvious... I should have tried this earlier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top