Setting focus to window

G

Guus Ellenkamp

I want to use a second window to link to and put new content in it and pop
up is some link is clicked. How to set the focus to the second window if
it's already open. With what I do now, the window stays below my main
window.
 
L

Lasse Reichstein Nielsen

Guus Ellenkamp said:
I want to use a second window to link to and put new content in it and pop
up is some link is clicked. How to set the focus to the second window if
it's already open. With what I do now, the window stays below my main
window.

I would use a function like

function winOpenFocus(url,name,config) {
var w = window.open(url,name,config);
w.focus();
return w;
}

and then use that where you use window.open now.

/L
 
G

Guus Ellenkamp

I think I found the solution myself: the window name may not contain
underscores.

Guus Ellenkamp said:
Thanks for the reply, but it doesn't work. I'm working with Dreamweaver.
This is the code I get:

<td width="21%" align="center" height="14"><font face="Arial, Helvetica,
sans-serif"><b><a href="http://www.abnamro.com"
onClick="MM_callJS('CompanyWindowHandle =
winOpenFocus(\'http://www.abnamro.com\',\'GE_Company\',\'\');')"
target="GE_Company">ABN-AMRO</a></b></font></td>
 
G

Grant Wagner

You need to return false from the onclick event, otherwise the HREF will be
followed and the default behaviour of the browser will take precedence.

<a href="http://www.abnamro.com"
onClick="MM_callJS('CompanyWindowHandle =
winOpenFocus(\'http://www.abnamro.com\',\'GE_Company\',\'\');');return false;"
target="GE_Company">ABN-AMRO</a>

Guus said:
Thanks for the reply, but it doesn't work. I'm working with Dreamweaver.
This is the code I get:

<td width="21%" align="center" height="14"><font face="Arial, Helvetica,
sans-serif"><b><a href="http://www.abnamro.com"
onClick="MM_callJS('CompanyWindowHandle =
winOpenFocus(\'http://www.abnamro.com\',\'GE_Company\',\'\');')"
target="GE_Company">ABN-AMRO</a></b></font></td>

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
D

DU

Guus said:
I think I found the solution myself: the window name may not contain
underscores.

Nah... Window name can contain underscores. That's not your problem
really. You need to use a global variable to store the returned window
object reference and then make a focus() call if the window exists but
has not been closed.

I discussed your issue at this precise url:

http://www10.brinkster.com/doctorunclear/Netscape7/Popup/PopupAndNetscape7.html#RaiseLowerSetting

There is an interactive demo in that page. Just look for the code.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
L

Lasse Reichstein Nielsen

DU said:
His window object reference needs to be a global variable, otherwise
it's pretty useless.

I am not sure I understand what the problem is. Can you explain in
more detail?

If he tries to open a page a second time with the same name, the page
will just open in the same window, and it will gain focus. I see no
need for a global variable.
Working example at:
http://www10.brinkster.com/doctorunclear/BrowserBugsSection/Opera7Bugs/Opera7Bugs.html
See function OpenRequestedPopup(strUrl) in the source code.

I made the WindowObjectReference a local variable and removed all the
tests for it already existing ... and it still worked the same. The
only difference is that now it doesn't check whether you are asking
for the same URL twice.

/L
 
D

DU

Lasse said:
I am not sure I understand what the problem is. Can you explain in
more detail?

If he tries to open a page a second time with the same name, the page
will just open in the same window, and it will gain focus. I see no
need for a global variable.




I made the WindowObjectReference a local variable and removed all the
tests for it already existing ... and it still worked the same. The
only difference is that now it doesn't check whether you are asking
for the same URL twice.

/L

Then it deletes the previous window, then re-creates and re-opens it
again which is a very poor usage of user system resources.
In your function
function winOpenFocus(url,name,config) {
var w = window.open(url,name,config);
w.focus();
return w;
}

this is what happens if the window with the same url already exists.
DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
L

Lasse Reichstein Nielsen

DU said:
Then it deletes the previous window, then re-creates and re-opens it
again which is a very poor usage of user system resources.

It shouldn't need to reopen it.
In your function
function winOpenFocus(url,name,config) {
var w = window.open(url,name,config);
w.focus();
return w;
}

this is what happens if the window with the same url already exists.

If a window with the same name already exists, the url is opened into
this window. The page is reloaded, even if a page with the same url is
already in it, that is true, but the window is not recreated. It is
reused.

Whether it is important not to reload the same URL, that is a
different problem, that the original poster didn't mention (and that I
honestly hadn't though about). To prevent this, you do need to keep
a copy of the URL around, either as a global variable, or as a local
static variable.

/L
 
D

DU

Lasse said:
It shouldn't need to reopen it.




If a window with the same name already exists, the url is opened into
this window. The page is reloaded, even if a page with the same url is
already in it, that is true, but the window is not recreated. It is
reused.

Thank you for correcting me on this. Yes. The document is reloaded but
the window is not recreated.
Whether it is important not to reload the same URL, that is a
different problem, that the original poster didn't mention (and that I
honestly hadn't though about). To prevent this, you do need to keep
a copy of the URL around, either as a global variable, or as a local
static variable.

/L

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top