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

Need help setting focus back to a previous window 2

Status
Not open for further replies.

jazzz

Technical User
Feb 17, 2000
433
US
Here is the scenerio. I use the default Macromedia java script to open a New window when the user clicks a reference, which there may be many of.

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

if the user opens another window but then clicks back on the first reference and the window is still open I want it to pop back on top. I know this is a simple question utilizing .focus() but I am a newbie to all this and could use the help.

Thanks in advance!

Life's a journey enjoy the ride...

jazzz
 
Try this:

Code:
[b]var newWin;[/b]
function MM_openBrWindow(theURL,winName,features) { //v2.0
  [b]if(newWin && !newWin.closed)
     newWin.focus();
  else
     newWin = [/b]window.open(theURL,winName,features);
 }

I think that should do it. 'hope it does!

--Dave
 
Thank you Dave but it keeps the current window open only it won't allow me to open another window as a reference. Let's say I open reference 1 of 10 when I click on reference 5 it pops the first one up again not want I am looking for. If reference 1 is opened then they click #5 let them open that one. Now if they go to #1 it is already open so just give it focus. I hope that's clear?

Thanks.

Life's a journey enjoy the ride...

jazzz
 
Okay, try this:

Code:
var newWin [b]= new Array(10)[/b];
function MM_openBrWindow(theURL,winName,features[b],winNum[/b]) { //v2.0
  if(newWin[b][winNum][/b] && !newWin[b][winNum][/b].closed)
     newWin[b][winNum][/b].focus();
  else
     newWin[b][winNum][/b] = window.open(theURL,winName,features);
 }

Could this work for you? Would you always know the 'winNum'? Do you know how many windows there will be so you can make the 'new Array(#)' to the write number initially?

If this doesn't work exactly, it's pretty close. Let me know how it goes.

--Dave
 
Same results as before it keeps the first window open but it does give it focus.

Life's a journey enjoy the ride...

jazzz
 
Okay, I think I get you. You don't mind using the same window as long as the window changes to the newly-selected location, right?

Code:
var newWin;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(newWin && !newWin.closed)
  [b]{
     newWin.location = theUrl;[/b]
     newWin.focus();
  [b]}[/b]
  else
     newWin = window.open(theURL,winName,features);
 }
 
Still no go, I receive Error URL undefined. How about this if the reference window is open the user clicks on 1 so it is open. Now they click on reference 3 lets close 1 before 3 opens. I think that would be the best way to go.

Thanks for all your help.

Life's a journey enjoy the ride...

jazzz
 
Oops. The problem with the code I put in my last post was with capitalization. Try again with theURL (instead of theUrl, like I had it).

I think opening the new site in the currently opened window is the better way to go (saves time), but if we can't get that going and you want to do that close window/open new window thing, try this:

Code:
var newWin;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(newWin && !newWin.closed)
  {
     newWin.close();
  }

  newWin = window.open(theURL,winName,features);
 }

We'll get this!

--Dave
 
That one works, I thank you so much. I am reading a programming book on javascript and I can read and understand what you have done except the line

if(newWin && !newWin.closed)

could you break that line down for me when you have time.

Thanks once again Dave, I truly appreciate the help.

Life's a journey enjoy the ride...

jazzz
 
if(newWin && !newWin.closed)


I'll assume that you understand the 'if' and the '&&'. So you know that the 'success' of the if-statement depends on both 'newWin' and '!newWin.closed' being regarded as true.

To say if(newWin) is to say "if the variable newWin has a value". Declaring 'var newWin;' isn't enough. Until the line where newWin is assigned a value, if(newWin) will return false.

JavaScript is funny in that all different types of variables can be represented by 'var'. It's up to us as programmers to remember what is what. Because newWin is a window, it has a property called 'closed' that is true if the window is closed. If newWin was a number or a String (based on my limited testing), it will always return false as long as newWin has been assigned a value (i.e., is forever regarded as open), but because, in this case, it is a window, the value gets wiped out (think: 'set to null') when the window is closed. Of course, the exclamation point (!) means 'not', so "!newWin.closed" means exactly that: "window newWin is not closed".

So, the whole statement 'if(newWin && !newWin.closed)' means: "if variable newWin has been assigned a value AND it is not closed, then enter the if-block". I guess, it might be more correct to say: "if variable newWin has been assigned a value AND that value is a window AND that window is not closed, then enter the if-block".

You might wonder why we check newWin by itself and don't just write: if(!newWin.closed). The reason is that this statement would probably (I think 'definitely') return an error if newWin has not been assigned a value.

Make sense?

'glad it finally worked!

--Dave
 
Thank you Dave it is very clear. I use vbscript in ASP and am familiar with the statements you mentioned. When I was attempting to use javascript I was trying to check for Null or "" (no value) I didn't know or realize that all I needed to do was if(newWin) would return false I did attempt to check closed as you mentioned above and it did error.

I truly appreciate all the help it makes it clear. I don't simply want an answer to a problem I want to understand the solution so I can tackle it in the future.

Regards
Myron
mkervin@epix.net

Life's a journey enjoy the ride...

jazzz
 
Hey LookingforInfo,
Once again one of your posts helped. I have been trying to solve the pop-up focus thing for three days now. I have help instructions that I need to load into 'helpwindow'. The thing was that everytime I went back to the parent window, I lost focus on the child for good. It updated but never came back into focus.

Your code:
Code:
var newWin;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(newWin && !newWin.closed)
  {
     newWin.location = theURL;
     newWin.focus();
  }
  else
     newWin = window.open(theURL,winName,features);
 }

....seems to work great. (I have updated the case in this copy.)

Thanks
Dave

"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
Hey, Dave!

You must be better at using the search engine at Tek-Tips than I am! I never seem to be able to find exactly what I'm looking for!

'glad you found what you needed!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top