window.opener not correct when window reopened

D

Dave

I see this problem in IE but not Firefox.

I have a page that opens a second page as a dialog box. If the Dialog
is relaunched from a second instance on the page the Dialog window is
reused, the controls are reinitialized but window.opener still refers to
the original opener. I have simplified code than demonstrated the
problem.

This is the code for the base window which launches the dialog called
Base.html:

<html><head><title>Base</title>
<script language="javascript">

function launch(){

var w = window.open(
"dialog.html",
"Dialog",
"width=400,height=389,scrollbars=no,menubar=no");
w.focus();

}

function callBack(){
alert("callBack : "+document.getElementById("name").value);
}

</script>
</head>
<body>
<input type="text" id="name"/n>
<input type="button" onClick="launch()" value="Launch" />
</body>
</html>

You see it has a text box, a button and a couple of javascript
functions. When you click the button the Dialog window is launched.

The callBack function is called by the dialog window and throws up an
alert with the contents of the text box. If you type different text
into the text box of both you can then tell which one threw up the alert
box.

Here's the code for Dialog.html:

<html><head><title>Dialog</title>
<script language="javascript">

function linkBack(){

window.opener.callBack();

}

</script>
</head>
<body>
<p>Dialog</p>
<input type="button" onClick="linkBack()" value="Link Back"/>
</body>
</html>

Here we have a button and a javascript function that's called when the
button is clicked. The Javascript function calls through window.opener
to teh callBack function in the opener.

In IE, launch 2 instances of Base.html. In the text box of one of them
type X and click "launch". Then go to the 2nd instance, type Y and
click "Launch". There will only be one instance of the Dialog but click
the "Link Back" button in the Dialog and "callBack : X" will be
displayed.

You could throw some javascript in the onload method of the body tag is
you want to convince yourselfe that the Dialog is actually being
reloaded.

Why isn't the window.opener property updated?
 
L

Lee

Dave said:
I see this problem in IE but not Firefox.

I have a page that opens a second page as a dialog box. If the Dialog
is relaunched from a second instance on the page the Dialog window is
reused, the controls are reinitialized but window.opener still refers to
the original opener. I have simplified code than demonstrated the
problem.
function launch(){

var w = window.open(
"dialog.html",
"Dialog",
"width=400,height=389,scrollbars=no,menubar=no");
w.focus();

}
Why isn't the window.opener property updated?


IE is apparently noticing that you're loading the same URL
in the window, and so is doing nothing. If you modify your
base.html so that you can choose to load a different URL
based on a checkbox state, you'll see that the opener is
updated if the URL is different.

A workaround would be to set w.opener=self in your launch()
function. Put it after the call to focus(), so my suggestion
won't be the line that fails if the window hasn't opened, yet.
 
L

Lee

Lee said:
Dave said:




IE is apparently noticing that you're loading the same URL
in the window, and so is doing nothing. If you modify your
base.html so that you can choose to load a different URL
based on a checkbox state, you'll see that the opener is
updated if the URL is different.

A workaround would be to set w.opener=self in your launch()
function. Put it after the call to focus(), so my suggestion
won't be the line that fails if the window hasn't opened, yet.

A more reliable work around might be to simply ensure that the
URL is always unique (unless you're very fast)[untested]:

var w = window.open(
"dialog.html?"+(new Date()).getTime(),
"Dialog",
...
 
D

Dave

I will try your suggestion but it seems that the page is reloading
because I know that the body's onLoad method will be called.




Lee said:
Dave said:




IE is apparently noticing that you're loading the same URL
in the window, and so is doing nothing. If you modify your
base.html so that you can choose to load a different URL
based on a checkbox state, you'll see that the opener is
updated if the URL is different.

A workaround would be to set w.opener=self in your launch()
function. Put it after the call to focus(), so my suggestion
won't be the line that fails if the window hasn't opened, yet.

A more reliable work around might be to simply ensure that the
URL is always unique (unless you're very fast)[untested]:

var w = window.open(
"dialog.html?"+(new Date()).getTime(),
"Dialog",
...
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
A more reliable work around might be to simply ensure that the
URL is always unique (unless you're very fast)[untested]:

var w = window.open(
"dialog.html?"+(new Date()).getTime(),
"Dialog",
...

In the presence of clock-correcting software, time can repeat. It might
be safer to define globally var Rabbit = 0 ; and to use
"dialog.html?" + Rabbit++ ;

That's not reliable, though, if the same dialog.html is opened from
different local pages.
 
D

Dave

A workaround would be to set w.opener=self in your launch()
function. Put it after the call to focus(), so my suggestion
won't be the line that fails if the window hasn't opened, yet.

This suggestion seems to have worked both in the sample application I
posted and in teh real application it was derived from.

Thanks a lot.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top