views:

43

answers:

4

Hi all,

We are use window.open for open popup. But then we want find it and close. Unfortunately we can`t save this popup handle to variable.

P.S. How get list of all windows?

+1  A: 

This should work:

var wh = window.open(..)

wh is the handle to the popup window.

Andrey
You'll note that the question mysteriously claims that they cannot do that.
Pointy
yes and not. A can`t have possible save handler
Diesel Draft
What do you mean? Why can't you save that handle? What's the problem?
Andrey
We can`t save it because this is not my script. We have only window name :(
Diesel Draft
Wow. that's insane.
Jacob Relkin
I don't think you can enumerate other browser windows - that would be a hacker haven :) You need to either modify that script or ask the vendor to modify it to return window handle. I don't see any other way.
Andrey
A: 

If you have control over the page that loads the script, you could do something like this. Warning: this is a really scary and generally bad thing to do:

<script>
  var windowHandles = {};
  (function() {
    var realOpen = window.open;
    window.open = function(url, name, features) {
      windowHandles[name] = realOpen(url, name, features);
    };
  })();
</script>

That will build an object (windowHandles) in which the handles for each opened window will be saved.

Put that script in your page before the script that opens the other window is loaded.

Pointy
You're right, it's a really bad solution - yesterday I spent two hours debugging a client's issue with String.split() acting weirdly on one particular page, just to find that some smart butt has overridden this standard javascript function. But if you REALLY can't have the script modified and ABSOLUTELY have to use it, I might go for it :)
Andrey
A: 

I don't like this solution. Fixing the script to give you a handle would be a better bet.

<button onclick="go()">Go</button>
<button onclick="stop()">Stop</button>

<script type="text/javascript">

    function go() {
            // Existing function. It opens a window with a name.
            window.open('http://google.com', 'test', 'width=300,height=300');
    }

    var foo;

    function stop() {
            // Open a new window with the same name. It replaces the existing window.
            // Since it opens a local document, the Same Origin Policy does not apply.
            // ... and we can capture its return value to grab a handle on an existing 
            // window
            foo = window.open('black-local-page.html', 'test',  'width=300,height=300');
            // Give the local page time to load
            setTimeout(continue_stopping, 500);
    }

    function continue_stopping() {
            // Call window.open() on the window
            foo.close();
    }

</script>
David Dorward
that'll work as long as the name is something useful and more-or-less unique, right?
Pointy
I didn't understand this, can you explain?
Andrey
@Pointy - names have to be unique.
David Dorward
@Audrey - I've added JS comments
David Dorward
Oh well duhh I guess that makes sense - however I don't know what happens if I give a page (in a popup) the name "Snowball", and then I reload a main browser tab with a different page using that same name.
Pointy
@Pointy — so long as you don't change domains, they'll share the window AFAIK.
David Dorward
@David well I don't see how that can work - windows can name themselves, so what happens when a window gives itself the same name as another existing window? I'll try it and see.
Pointy
A: 

I found not perfect solution, but it work.

win = window.open(null, 'Window1');

This code search search window with this name and return handler, but if window is closed it open empty popup. I Think this is temporary solution

Diesel Draft