PDA

View Full Version : Getting every window as an Array?



???
07-24-2007, 04:41 PM
I was wondering if you could get an array or something of every window, or an array of the names. I was looking at some of Firefox's js and they had some stuff for tabs that was like,

this._windows[<something goes here>]
but when I tried it, it didn't work. Any other possibilities?

Trinithis
07-25-2007, 02:16 AM
var windows = [window];
for(var i=0; i<window.frames.length; ++i) windows.push(window.frames[i]);

Trinithis
07-25-2007, 08:11 PM
Oh, and when you open a new window through window.open(), add the new window to the array.

Twey
07-25-2007, 09:55 PM
You can override window.open to do that automatically:
open = (function(oldopen) {
return function() {
return (windows[windows.length] = oldopen.apply(window, arguments));
};
})(open);

???
08-02-2007, 02:09 AM
Wait, it says that window.frames.length is equal to 0, so how does that work?

Twey
08-02-2007, 04:54 PM
That just means you have no frames on the page.

???
08-03-2007, 05:11 PM
Frames? I was wondering about other browser windows. How do frames play in? I had some tabs open.

jscheuer1
08-03-2007, 05:45 PM
A frame, if it exists, is a window object.

???
08-03-2007, 05:53 PM
Well then does that work in reverse? Were if a window exits its part of window.frames? Because I don't want every frame, I want every window.

jscheuer1
08-03-2007, 06:18 PM
No, a full window is not a part of the window.frames object. If you don't want any frames or iframes, the window.frames object may be excluded. I haven't been following this thread closely so, I'm not sure if it would have to be actively excluded or merely remove from consideration. Most likely you could just leave it out, testing would be a good idea though.

However, if you leave out frames, you do not technically have all 'windows'. This may come back to haunt you at some point, or not.

Additionally, windows not created with javascript and not existing as frames in the window the code in this thread is on, would not be included in the array. I don't think there is any way to just get all windows, like if the user arrives at your page with several windows open from another domain or domains, as far as I know, those cannot be found or counted with javascript on your page.

???
08-06-2007, 03:53 AM
How efficient would be using one Function statement? To check all the possibilities up to a certain length. And what is the max length for window names?

jscheuer1
08-06-2007, 04:08 AM
Very inefficient. I'm not aware of any limitation, though there must be one. I'm sure it is high. I remember one time we did a little project here using just 6 letters (window names may include numbers and some other non-letter characters). We didn't even generate all possible combinations. We went from aaaaaa to zzzzzz, covering only every sequential combination. It really bogged down the browser. And we weren't even comparing them to anything, just generating them.

???
08-06-2007, 04:47 AM
Well I guess that wouldn't work but it's pretty funny. You would have to to soooo many.

Twey
08-06-2007, 04:10 PM
It really bogged down the browser.This could be avoided by using a setTimeout(). It would still take ages, though.