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

Bringing an existing popup to front without refreshing...

Status
Not open for further replies.

pjfarr

Technical User
Dec 1, 2001
45
CA
How can I bring on open popup window to the front of all other windows without refreshing its contents by clicking a link in a different popup (both popups launched from the main window)?

This has stumped me and several others I've asked...
Is there an answer...?

~P.J.F.
 
I have never tried doing it myself but it might be possible.
When you open the popup window you give it a name. Try using that name to set the focus on the popup window.


Stamp out, eliminate and abolish redundancy!
 
theniteowl has the right idea - here's some code I've used in one of my applications to open up a window, or set focus to it if it's already opened:
Code:
<script type="text/javascript">

var popupWindow;

function openPopup() {
   try {
      popupWindow.focus();
   }
   catch(e) {
      popupWindow = window.open('[URL unfurl="true"]http://www.google.com');[/URL]  
   }
}

</script>
<input type="button" value="google window" onclick="openPopup()" />

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Thanks guys for your input.

The sample code you provided Kaht opens up another instance of the same popup (so there's two of the same). Maybe I did it wrong though. I placed the code in the 2nd popup that's supposed to bring the first (already open) popup to the front. That seemed like the obvious thing to do. I do know already how to get an existing popup to come to the front; what I can't figure out is how to do it without the popup "refreshing" (and thus losing the info the user has input).

*whew*
Thanks if anyone can solve this one (and for making it through my long-winded prose).

~P.J.F.
 
So the 2nd popup is opened from the 1st popup, and code on the page of the 2nd popup is supposed to set focus to the 1st popup? (you didn't say that in your first post)

Use this code instead:
Code:
function focusPopup() {
   try {
      [!]opener[/!].focus();
   }
   catch(e) {
      //do nothing if window has been closed
   }
}

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Hmmm...no. Sorry for not being more articulate, Kaht.
Here's what I'm essentially trying to do (it's for a javascript-based game, in case you're wondering why the h*ll I would want to do such a convoluted thing):

• Main Window opens Popup #1 which contains a form user enters info into and will continue to do so from time to time throughout session (so it has to stay open, though not necessarily in focus).

• At different times, various links in Main Window open Popup #2, #3 or #4, etc. depending where the user clicks, each containing different info. I've got that much working already. What I need is:

• Ideally, when any of these other Popups are closed (#'s 2, 3 and 4, etc.), I would then like Popup #1 to automatically come to the front as the user must enter info from the various other Popups into Popup #1, adding to any info the user previously entered there. So Popup #1 has to remain open and must not refresh its contents when it comes to the front each time, as the user's info has to remain for the duration of the session. (Basically, the same as if he had clicked Popup #1's button on the taskbar to regain access to it.)

That's about it. At any rate, thanks for your interest. I've stumped about everyone (including myself).

~P.J.F.
 
You could put an onunload handler in all of the popups which would focus the parent window. If this doesn't work, you may need to call a function in the parent window to do this after a slight delay.

If you find that not all browsers support onunload, then you should set up a timer in the parent window that polls all windows opened by it (see my FAQ on closing all child windows on how to keep track of windows opened), and checks their "closed" status. If closed, you can focus the parent window (you'll need to them remove the window reference from the array mentioned in the FAQ, otherwise the parent window will be forever getting focus).

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for your input BRPS (love that ID)

The problem is automatically bringing Popup #1 back into focus each time the secondary popups are closed, not bring focus back to the parent window. I can get Popup #1 to come to the front alright, but the page always reloads when doing so and thus loses the info the user has previously entered there. So I'm trying to get around Popup #1's reloading every time it's brought into focus.

~P.J.F.
(By the way, love that aquarium thing on your site - cool.)
 
How are you bringing it into the foreground at the moment? Using the focus method should not reload the page (unless you have specifically added an onfocus method that does so)... so I can only imagine you are using code that explicitly does this.

If you use the second method I described, it would be really easy to focus popup1 instead of the parent.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I first tried putting the OnFocus in the parent window so that when the 2nd popup closed it would bring the parent window back into focus and thus launch the first popup. However, every time anything was done in the parent window, the 1st popup would come to the front (naturally), which was annoying and also it refreshed the popup and lost the info anyway.

Then I tried putting the OnFocus in the popups that were to launch the first popup on close. Same thing, brought the first popup to the front but refreshed (and thus lost) the contents.

I'm thinkin' the whole thing's likely a pipe dream and I should just let the user click the d*mn taskbar button to access the first popup...but I'm a little obsessive about these things and once the notion's in my head, well...

~P.J.F.
 
As I said:

self said:
Using the focus method should not reload the page ([!]unless you have specifically added an onfocus method that does so[/!])... (or) [!]you are using code that explicitly does this[/!].

Can you show the code you have? You [!]must[/!] be reloading, refreshing, or reopening the popup if it is reloading. This is [!]not[/!] default behavaiour that happens when you focus a window.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Please forgive my being a dunce, Dan.

The main window has Popup #1 open initially via the OnLoad string, referenced in this script (which also closes the popup when the main window is shut down):

<SCRIPT>
var windowHandle = -1;
function popupWindow1() {
windowHandle = window.open('popup1.htm','popup1','width=200,height=390','top','left','front');
windowHandle.focus();
}
function closeAll() {
if (windowHandle != -1)
windowHandle.close();
setTimeout('self.close()',500);
}
</SCRIPT>

I placed the same code in all the secondary popups which are supposed to bring Popup #1 back up front when they are closed, by placing the function "popupWindow1();" in the OnUnload string of these secondary popups. This brings Popup #1 up front alright, but the info the user has entered is cleared.

I tried changing the "window.open" to "window.focus" but that only suppresses the popup altogether. If possible, can you provide a step-by-step (as if you haven't spent enough time on this nonsense already).

If possible, can you provide a step-by-step? (as if you haven't spent enough time on this nonsense already...)

Again, thanks for your interest and please forgive my dunce-ishness...
~P.J.F.
 
pjfarr, you are including the code to OPEN a window, not to bring it into focus.
The popupWindow1 function creates a new window where I believe you only want to execute a function to bring the existing window into focus. You need to use the focusPopup function above in your onunload event.




Stamp out, eliminate and abolish redundancy!
 
Thanks Owl,

For some reason I'm just not getting this...
How exactly is the focus function written and where in the code does it go? If I alter or take out the window.open setting in any way the popup doesn't open at all.

~P.J.F.
 
I do not fully understand the flow of your app.
In what circumstance is the window popped open and under what circumstance is it closed?

You seem to be trying to use the code to create the popup from every page which means every page that loads tries to create NEW popup window, not bring it into focus.
You probably want to be using the focusPopUp function to set focus to the already created popup.

Am I right in thinking that you want the popup open ALL the time without it closing until they leave the application?
And you just want to set the focus based on other events?
If that is the case you do NOT want to put the popup code in every page, only in the main page.


Stamp out, eliminate and abolish redundancy!
 
Yes, Popup #1 is to remain open until the user closes the main window. (Loading of the main window launches Popup #1 initially.) Various links in the main window will bring up other separate popups with different info in each. When each of these other popups are closed, I'd like the first popup, #1, to come up to the front, since the info the user got from the various other popups is to be entered into the form fields in Popup #1. Since the user will be repeating this sequence numerous times, Popup #1 needs to retain all the info the user continues to add throughout the session but does not need to be in front until its needed.

Hope that's a clear enough summary of what I'm trying to do. It's for a javascript "boardgame" I've been developing. This is the final function I need to work out in order for the game to be finished. Popup #1 is the scoreboard, the other popups give the user points that are to be entered onto this scoreboard, which the user uses to keep a running total of.

Thanks again,
~P.J.F.
 
The popup code you have in each page has to give a distinct name to the window it opens.

I think the "refreshing" effect you were seeing was another browser window trying to open up a popup of the same name. I suspect it did not open a second one but re-used the existing one because both scripts tried using the same name.

So your main page should have a function to open a popup with a distinct name indicating it is related to the primary page. The main page should also have an onunload event that closes that same popup window.

The links that create new popup windows should create them with new distinct names. When those popups close you would have an onunload event that sets focus to the first popup window.



Stamp out, eliminate and abolish redundancy!
 
No, Dan. Everyone's been great to try and help and I appreciate it. But for some reason I can't seem to get the actual code right. Everybody's been telling me to use 'focus', not 'open', and of course that makes sense, but I don't know how to write the actual string and where to put it.
I know how to do a 'window.open' function and I figured merely changing the word 'open' to 'focus' would work but no.

~P.J.F.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top