focus a window - how to

H

Helmut Jarausch

Hi,

I'm an absolute beginner with JavaScript.
Say, I am in a window which I call WinA
There I have
<script>
var WinA=self
</script>


Later on, while a different window ("WinB") is focused, I tried
<script>WinA.focus()</script>

but it doesn't give focus to the previous Window (WinA).

Are variables in JavaScript "global" or local to a given
window. ( If yes, are there global variables? )

Sorry, if this is a too dumb a question.

Many thanks for your help,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
E

Erwin Moller

Helmut said:
Hi,

I'm an absolute beginner with JavaScript.
Say, I am in a window which I call WinA
There I have
<script>
var WinA=self
</script>


Later on, while a different window ("WinB") is focused, I tried
<script>WinA.focus()</script>

My guess is that javascript complains that WinA is unknown?
(Check in firefox the javascriptconsole)

but it doesn't give focus to the previous Window (WinA).

Are variables in JavaScript "global" or local to a given
window. ( If yes, are there global variables? )

They are global to the window they live in.
The good news is that you CAN communicate between windows.
The URLs in both windows MUST be in the same domain for this to work.

example:
you only have WinB.
You open WinA from WinB, like this:

var myWinAreference =
window.open("http://www.example.com/test.html","WinA");

Now you have a variable named myWinAreference in your WinB.

You can use this myWinAreference to address WinA, eg:
myWinAreference.sayHi();
will call a function sayHi() in WinA.

or:

myWinAreference.focus();

Hope that helps.
Regards,
Erwin Moller
Sorry, if this is a too dumb a question.

No such thing as a dumb questions. Dumb answers however... ;-)
 
T

Thomas 'PointedEars' Lahn

Helmut said:
Say, I am in a window which I call WinA
There I have
<script>
http://validator.w3.org/

var WinA=self

You are relying on the presumption that both the Global Object refers to
a Window object and that this Window object has a host-defined `self'
property that refers to itself. Such an approach is error-prone at best.
It would be best to presume as little as possible:

var winA = window;

In fact, the existence of `window' as a host-defined property of the Global
Object to refer to the current Window object should also be tested for
before its being used. One is part of the language, the other is part of
the AOM (Application Object Model) of the UA (user agent).
</script>

Later on, while a different window ("WinB") is focused, I tried
<script>WinA.focus()</script>

Where *exactly*?
but it doesn't give focus to the previous Window (WinA).

Because of abuse by scriptkiddies in the past, current Web UAs have an
option to prevent script applications from focusing or hiding windows by
rendering the corresponding method calls not functional. Maybe you have
that option enabled.
Are variables in JavaScript "global" or local to a given
window. ( If yes, are there global variables? )

That depends where, i.e. in which scope they have been declared. Global
variables are global to the execution context. It just happens that this
execution context is mostly window-based in graphical Web UAs.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Erwin said:
Helmut Jarausch wrote:
[...]
The good news is that you CAN communicate between windows.
The URLs in both windows MUST be in the same domain for this to work.

No, the Same Origin Policy is only restricted to certain operations.
example:
you only have WinB.
You open WinA from WinB, like this:

var myWinAreference =
window.open("http://www.example.com/test.html","WinA");

This will open a new window *almost* always and return a reference to the
corresponding Window object, as there is no window *named* "WinA" (reference
name != window name). Assigning "WinA" to window.name when execution code
in the document of winA before winB is opened may work, but is not required
to. But that is not what the OP is looking for.
Now you have a variable named myWinAreference in your WinB.

You can use this myWinAreference to address WinA, eg:
myWinAreference.sayHi();
will call a function sayHi() in WinA.

or:

myWinAreference.focus();

One would want to use window.opener as `myWinAreference' in winB because
that is what refers to the Window object of winA, provided winB is opened by
a script in winA, as the OP stated. Neither will prevent possible blocking
of that operation by the UA, though.
No such thing as a dumb questions. Dumb answers however... ;-)

Indeed ;-)


PointedEars
 
H

Helmut Jarausch

Erwin said:
My guess is that javascript complains that WinA is unknown?
(Check in firefox the javascriptconsole)



They are global to the window they live in.
The good news is that you CAN communicate between windows.
The URLs in both windows MUST be in the same domain for this to work.

example:
you only have WinB.
You open WinA from WinB, like this:

var myWinAreference =
window.open("http://www.example.com/test.html","WinA");

Now you have a variable named myWinAreference in your WinB.

You can use this myWinAreference to address WinA, eg:
myWinAreference.sayHi();
will call a function sayHi() in WinA.

or:

myWinAreference.focus();

Thanks for your help. I fear I've a more complicated situtation.

On my initial page I have
<frameset rows="60%,*">
<frame src="Quest.pih" name="Questionnaire">
<frame src="empty.html" name="help">
</frameset>


in Quest.pih <form method="post" action="Search.ks/Show" target="Results">
where there is no frame with name "Results", yet. So my browser
opens a new one. There I want a button "new question" which gives
focus to the previous page which contains the frame "Questionnaire".

So, the window opening is implicit! What can I do?

Many thanks,
Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
T

Thomas 'PointedEars' Lahn

Helmut said:
in Quest.pih <form method="post" action="Search.ks/Show" target="Results">
where there is no frame with name "Results", yet. So my browser
opens a new one. There I want a button "new question" which gives
focus to the previous page which contains the frame "Questionnaire".

So, the window opening is implicit!

If it even happens.
What can I do?

For example:

<script type="text/javascript">
function submitPopup(f)
{
var w = window.open(f.action, f.target, "...");
f.submit();
return !!w;
}
</script>

<form method="post" action="Search.ks/Show" target="Results"
onsubmit="return !submitPopup(this);">

This presumes that the window name is available right after window.open()
was called; however, that operation is performed by the AOM and so
asynchronously. Using a timeout before submitting the form might be
necessary, although not entirely reliable as well.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
<script type="text/javascript">
function submitPopup(f)
{
var w = window.open(f.action, f.target, "...");
f.submit();
return !!w;
}
</script>

<form method="post" action="Search.ks/Show" target="Results"
onsubmit="return !submitPopup(this);">

This presumes that the window name is available right after window.open()
was called; however, that operation is performed by the AOM and so
asynchronously. Using a timeout before submitting the form might be
necessary, although not entirely reliable as well.

With winB then opened through scripting, you have a reference to it in the
document of winB with `window.opener'. Since you can't be sure how the
document was accessed, you should test that property:

<script type="text/javascript">
function focusOpener()
{
var wOp = window.opener;
if (wOp && !wOp.closed
&& /\b(function|object)\b/.test(typeof wOp.focus) && wOp.focus)
{
wOp.focus();
}
}

document.write('<input type="button" value="New Question"'
+ ' onclick="focusOpener()">');
</script>


HTH

PointedEars
 

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,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top