Forum Moderators: open

Message Too Old, No Replies

Disappearing windows

Window opened from window.open() gets covered up.

         

hunkymonkey

7:30 pm on Sep 24, 2004 (gmt 0)

10+ Year Member



I have a setup where from my main page, you can click on a link, and it takes you to another page that calls window.open to open a new window then does a history.back() to return the main window back to the main view.

But on a lot of systems, as soon as the main window goes back to the main view, it covers up the window I just opened.

Is there any way to keep the new window on top? I've tried several things, like recapturing focus any time it's lost (problem-it becomes a modal window), capturing focus after a short period of time, like 1 second (problem-I still want to be able to allow the user to click on the main window and view it without the second window covering it up later). And I've even tried to put something in the main page the opens the window from the main page if a certain url parameter is passed in. But that causes the window to be opened every time you refresh or go to a different page and then back again.

Help!

Rambo Tribble

10:45 pm on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried transferring control to a script in the second window, fired by its onload event, that causes the opener to go back and retains focus on the second window?

hunkymonkey

11:41 pm on Sep 24, 2004 (gmt 0)

10+ Year Member



You mean, rather than have my page #2 in my main window calling:
window.open(...)
history.back()

having my main window call:
window.open(...)

And then my new window:
<body onload="parent.history.back();">
?

No, I never thought of that. I'll give it a shot. But from past experience, I think if I reload the second window (i.e. hit F5), it will also take the main window back yet again, and I don't want that.

Is there a way I could set a status variable that doesn't get reset when the page gets reloaded?

hunkymonkey

12:07 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Well, I'm trying. It's been years since I was good at Javascript, and I usually try everything I can think of until it works. So probably most of what I come up with is not the "best" way to do it.

I'm trying to handle making the top window go back a page when my second window opens. Something like this:
<body onload="parent.history.back();">

But nothing happens. It looks like "parent" and "this" are the same thing here. What's going on? How do I alter the main window? Does it have to have a name?

hunkymonkey

12:27 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Okay. I've figured this much out. I can sent my main window's parent to the subwindow by doing this:
sw=window.open(...);
sw.mainWindow=parent;

And then in the subwindow I can set history back like this:
mainWindow.history.back();

However, if I reload, the mainWindow object no longer exists in my subwindow. Is there a javascript function to check whether a variable exists so that I don't pop up any error boxes?

Bernard Marx

12:36 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"parent" and "this" are the same thing here

They are. For the main window, you want

[blue]opener[/blue]
.

Rambo Tribble

1:51 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, parent is a reference in the frames context; opener is the equivalent in the window context.

hunkymonkey

3:57 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Okay, but to reiterate my last question, I want to pass in a reference to the main window, and then do a main.history.back() in the onload handler of my subwindow. But if the subwindow is refreshed, the reference is no longer there, and I get a popup error. Can I check to see if the reference is there without checking the value of it, which causes the error?

I would just do opener.history.back() or whatever is right, but then if the subwindow got refreshed, it would send the main window backward yet again, and I don't want that.

Rambo Tribble

1:23 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One possibility is to load an intermediate page into the second window that initiates history.back() then loads the actual page you want in that second window, its code having no script for history.back.

hunkymonkey

5:15 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



I like your thinking, because that's the way I started out thinking. But that's what caused the problem in the first place. The original solution has an intermediate step which first opens up another window and then calls history.back(). The problem with that is that the new window disappears (behind the main window). I'd assume that's because window.open() gets called before history.back() gets called, which causes the main window to recapture focus. But if history.back() gets called first, then window.open() never happens!

Now, I suppose I could pop up two windows, one of which would just call history.back() on the first window, pop open a third window, and then close itself, which would probably work. But I'm a big fan of simple solutions, so 3 html pages to pop up one window seems like overkill.

So unless someone knows how to check for the existence of a variable without causing a runtime error, I'm going to just leave it the last way I did it: The main window will send a reference to itself to the second window after popping it up. The second window will call history.back() on the caller window, and then request focus. That will work most of the time, since I'm eliminating all the buttons and other functionality of the second window, so the only way they'll have to refresh is if some server error occurs and the ActiveX object that appears in the window (the only thing the window is for) doesn't show up. And if that happens, I expect most typical users would just close the window and try the original link again anyway. Besides, a normal person might not think it's so strange to get a runtime error right after a server error.

Thanks.

Rambo Tribble

10:45 pm on Sep 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it might work if you do it in this order:

1) Open the popup and initiate a script in it that
2) initiates opener.history.go(-1) then
3) recaptures focus and changes window.location to the new content address, overwriting the existing code in the popup.