Second window

T

Tom de Neef

A user click will result in calling up and filling a secundary window. When
that secundary window already exists, it should be cleared so that the user
sees the window empty while waiting for a respons from the server. When the
secundary window does not yet exist, it should be created and focused and
then filled.
I have used this code:
{code}
var secondWindow = null;

function load2ndWindow(query) {
if (secondWindow) secondWindow.document.write(emptyDoc);
secondWindow =
window.open(query,"Support","menubar,resizable,width=600,height=300,scrollbars");
secondWindow.focus();
}
{code}

This works as long as the main page is not reloaded. But when it is
reloaded, the script is reloaded as well and the link between variable
secondWindow and the secundary window is broken.
I have two questions:
a) how come that the browser will re-use the secundary window when
load2ndWindow() is called after a page reload? It seems more reasonable when
it creates a new secundary window and leaves the existing one intact. As it
is though, this suits me.
b) how can I clear this secundary window that is about to be re-used by the
browser after the main page is reloaded and the secondWindow variable no
longer points to it?

I hope that I have described my problem adequately.
Thank you,
Tom
 
T

Thomas 'PointedEars' Lahn

Tom said:
A user click will result in calling up and filling a secundary window.
_secondary_

[…]
var secondWindow = null;

function load2ndWindow(query) {
if (secondWindow) secondWindow.document.write(emptyDoc);
secondWindow =
window.open(query,"Support","menubar,resizable,width=600,height=300,scrollbars");
secondWindow.focus();
}
{code}

This works as long as the main page is not reloaded. But when it is
reloaded, the script is reloaded as well and the link between variable
secondWindow and the secundary window is broken.
I have two questions:
a) how come that the browser will re-use the secundary window when
load2ndWindow() is called after a page reload?

Same window name (here: Support). RTFFAQ, RTFM, STFW.
It seems more reasonable when it creates a new secundary window and leaves
the existing one intact.

No, it does not. The previous global execution context along with the
global object and all its properties ceases to exist on reload of the
document view. So the DOM implementation has nothing to use as a reference
but the window name.
As it is though, this suits me.
b) how can I clear this secundary window that is about to be re-used by
the browser after the main page is reloaded and the secondWindow variable
no longer points to it?

You will have to use the same window name so that window.open() returns a
reference to the Window instance.

As it is, your approach is flawed. document.write() will always overwrite
the content if the document has been loaded, but it will not automatically
close the output stream (you need document.close() for that). window.open()
will always replace what you might have written with document.write()
before.

You should check if `secondWindow' refers to a Window (`secondWindow
converts to true) that is not closed (`closed' property is false), and only
call window.open() if neither is the case. Feature-test the focus() method
before you call it then, and catch any exceptions the call might still
throw.

We have been over this ad nauseam, and recently. Please search before you
post.


PointedEars
 
T

Tom de Neef

Thomas 'PointedEars' Lahn said:
You will have to use the same window name so that window.open() returns a
reference to the Window instance.

As it is, your approach is flawed. document.write() will always overwrite
the content if the document has been loaded, but it will not automatically
close the output stream (you need document.close() for that).
window.open()
will always replace what you might have written with document.write()
before.

You should check if `secondWindow' refers to a Window (`secondWindow
converts to true) that is not closed (`closed' property is false), and
only
call window.open() if neither is the case. Feature-test the focus()
method
before you call it then, and catch any exceptions the call might still
throw.

Thank you. But...

function load2ndWindow(query) {
if (secondWindow && !secondWindow.closed) secondWindow.clear();
secondWindow = window.open(query,"Support", ...);
}

Loading the query results from the server may take some time. I want to
avoid the situation where the user sees obsolete previous data in the
support window.
There are three situations to consider:
a) a support window does not exist (never created or closed by user). The
function works fine. An empty support window will show up. Browser waits for
data and then shows it.
b) the support window exists and the secondWindow global variable is
associated with it. The function works fine. The support window gets the
focus (if needed that can be added to the code), is cleared and remains
virgin until new data arrives.
c) a support window exists but (due to reloading of the main page) the
global secondWindow has value null. What happens now is: the support window
gets the focus and shows obsolete data while waiting for the new data to
arrive from the server.

This last situation I want to avoid. Is there a way to associate the
variable secondWindow with an existing window, other than via window.open? I
now use:
//code//
function clear2ndWindow() {
if (secondWindow && !secondWindow.closed) {
secondWindow.document.body.innerHTML = ' ';
return true}
else return false
}

function load2ndWindow(query) {
// make sure second window is cleared
if (!clear2ndWindow()) secondWindow =
window.open('',"PRNinsightSupport","menubar,resizable,width=600,height=300,scrollbars");
secondWindow =
window.open(query,"PRNinsightSupport","menubar,resizable,width=600,height=300,scrollbars");
try {secondWindow.focus()} catch (e) {};
}
//code//

and this works for all three situations but it seems a bit daft to call
window.open('',...) prior to calling window.open(url,...).
(I will put a try..catch around the focus() call

Tom
 
T

Tom de Neef

Tom de Neef said:
There are three situations to consider:
a) a support window does not exist (never created or closed by user). The
function works fine. An empty support window will show up. Browser waits
for data and then shows it.
b) the support window exists and the secondWindow global variable is
associated with it. The function works fine. The support window gets the
focus (if needed that can be added to the code), is cleared and remains
virgin until new data arrives.
c) a support window exists but (due to reloading of the main page) the
global secondWindow has value null. What happens now is: the support
window gets the focus and shows obsolete data while waiting for the new
data to arrive from the server.

This last situation I want to avoid. Is there a way to associate the
variable secondWindow with an existing window, other than via window.open?
I now use:
//code//
function clear2ndWindow() {
if (secondWindow && !secondWindow.closed) {
secondWindow.document.body.innerHTML = ' ';
return true}
else return false
}

function load2ndWindow(query) {
// make sure second window is cleared
if (!clear2ndWindow()) secondWindow =
window.open('',"PRNinsightSupport","menubar,resizable,width=600,height=300,scrollbars");
secondWindow =
window.open(query,"PRNinsightSupport","menubar,resizable,width=600,height=300,scrollbars");
try {secondWindow.focus()} catch (e) {};
}
//code//

and this works for all three situations but it seems a bit daft to call
*** Oeps, sorry it does not work in situation c ***
I had forgotten to remove a clear2ndWindow() which is called in
windows.onunload.
I give up
Tom
 
T

Thomas 'PointedEars' Lahn

Tom said:
Thank you. But...

function load2ndWindow(query) {
if (secondWindow && !secondWindow.closed) secondWindow.clear();
secondWindow = window.open(query,"Support", ...);
}

Loading the query results from the server may take some time. I want to
avoid the situation where the user sees obsolete previous data in the
support window.

I did not think of that. Your approach is sound then if you close the
output stream. However, you might want to write a complete HTML document
then, as I am not sure that all browsers would automatically wrap the text
in one. And never have a textual empty document; this is not user-friendly.
But see below.
There are three situations to consider:
[…]
c) a support window exists but (due to reloading of the main page) the
global secondWindow has value null. What happens now is: the support
window gets the focus and shows obsolete data while waiting for the new
data to arrive from the server.

This last situation I want to avoid. Is there a way to associate the
variable secondWindow with an existing window, other than via window.open?

No, but there is another possibility: You can focus an existing popup window
only when its content has been loaded, preferably in the `load' listener of
the new support document's body (`onload' attribute value).


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[…] You can focus an existing popup window only when its content has been
loaded, preferably in the `load' listener of the new support document's
body (`onload' attribute value).

But this is not very user-friendly, too, for the user might wait a long time
without seeing any reaction to their action. I can see no viable
alternative to it, though.

It would probably be best if you indicated to them that they need to wait
some time. Possibilities that spring to mind is a "loading" message in the
opener and a change of pointer cursor in the opener issued by popup code
that is reverted by popup code when everything has been loaded.


PointedEars
 
T

Tom de Neef

Thomas 'PointedEars' Lahn said:
[.] You can focus an existing popup window only when its content has been
loaded, preferably in the `load' listener of the new support document's
body (`onload' attribute value).

But this is not very user-friendly, too, for the user might wait a long
time
without seeing any reaction to their action.
Indeed.

I can see no viable alternative to it, though.

It would probably be best if you indicated to them that they need to wait
some time. Possibilities that spring to mind is a "loading" message in
the
opener and a change of pointer cursor in the opener issued by popup code
that is reverted by popup code when everything has been loaded.

I agree. Thank you.
Tom
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top