PDA

View Full Version : Javascript 'newwindow.opener': how to 'focus()'?


techwannabe
04-30-2007, 02:33 PM
Hello.

In Javascript, I created a window via window.open() function. I also use the newwindow.opener so links in the new window open in the parent window.

The problem is, the opener window stays hidden behind others when multiple windows are open.

Q#1: What is the syntax to focus that opener window?

Q#2: What is the syntax for the link in the new window to create that window if closed? Then, what syntax would I use for the same link in the new window for that condition also?

Thanks.

dang
04-30-2007, 04:29 PM
I'm confused on your question #2. A link in the new window wont work if that new window is closed... Can you try describing it again?

To focus a window:
when you use window.open, assign the object to a variable name, ie:

popUp = window.open(stuff here);

Then assign focus:
popUp.focus();

-dan

techwannabe
04-30-2007, 05:12 PM
Thanks, Dang.

It's not the open() but 'opener' e.g. newwindow.opener = window which refers to the window that opened the newwindow.

I have it working for the newwindow's content to display in the 'opener' window, but I just can't get it to focus.

Question#1 looks backward; question#2 will be looking forward. To handle both, I will create a function to incorporate both conditions, but I need both to immediately 'focus' onclick. Need syntax for Q#1. I believe that I have the answer to Q#2.

Thanks again.

EDIT: I have tried using the target="..." in the link with onClick="newwindow.opener.focus()" but that didn't work. Must the 'target' be converted to Javascript in order for the focus to work? The content is writing to the 'target' which is what I want.

dang
04-30-2007, 09:36 PM
Ahh, I see. You want to throw a pop-up that is hidden behind the parent?

Use .focus to set which ever window you want it to focus to.

Just use:
this.focus();

in whatever page you want focus to. Example of quick and dirty code:

<html>
<head>
<script type="text/javascript">

function popUp()
{
var newwindow = '';

newwindow = window.open('http://www.google.com/', 'test', 'height=150, width=150');
this.focus();
}
</script>
</head>

<body>
<a href="#" onclick="popUp();return false;">Test</a>
</body>
</html>

If you want the popup window to have focus, set newwindow.focus().

BTW, this page has a good example of how to check if a window's open already:
http://www.quirksmode.org/js/croswin.html

dang
10-16-2007, 02:38 PM
So does anyone find this useful?