Passing a JavaScript variable/object reference from one page to another?...

D

Davide Bruzzone

Greetings all...

Here's a description of the problem that I'm trying to solve:

- I have a Web page from which users can open one or more other
windows.
- From that web page, the user can then select another part of the
application thereby loading a different page into the original browser
window (from which one or more "child" windows may have been opened).
- When the user logs out of the application, regardless of whether
they are on the page on which they started (in the original window),
or they are in another part of the application, we need to be able to
close all the windows that the user opened from the original window.

We're using IE 6.0...

My problem is that I can't find a way to pass the object that points
to a window that was opened on one page into another page...

For example:

- I start out on a.html
- I open a window... My code looks something like this:

var myWindow = window.open(...

- In the original browser window (which is currently displaying
a.html) I click a link that takes me to b.html
- In b.html I may need to close myWindow, but I don't have a reference
to it, so I can't call myWindow.close()

Is there any way to pass the myWindow object into b.html (or to store
it in some "global" area from which it can be accessed by any page)?
Either that, or am I missing something far more obvious?

I've started thinking about a number of solutions to this problem, but
none of them are anywhere near elegant...

Any suggestions would be greatly appreciated...

Cheers...

Dave Bruzzone
 
G

Greg

Greetings all...

Here's a description of the problem that I'm trying to solve:

- I have a Web page from which users can open one or more other
windows.
- From that web page, the user can then select another part of the
application thereby loading a different page into the original browser
window (from which one or more "child" windows may have been opened).
- When the user logs out of the application, regardless of whether
they are on the page on which they started (in the original window),
or they are in another part of the application, we need to be able to
close all the windows that the user opened from the original window.

We're using IE 6.0...

My problem is that I can't find a way to pass the object that points
to a window that was opened on one page into another page...

For example:

- I start out on a.html
- I open a window... My code looks something like this:

var myWindow = window.open(...

- In the original browser window (which is currently displaying
a.html) I click a link that takes me to b.html
- In b.html I may need to close myWindow, but I don't have a reference
to it, so I can't call myWindow.close()

Is there any way to pass the myWindow object into b.html (or to store
it in some "global" area from which it can be accessed by any page)?
Either that, or am I missing something far more obvious?

I've started thinking about a number of solutions to this problem, but
none of them are anywhere near elegant...

Any suggestions would be greatly appreciated...

Cheers...

Dave Bruzzone


I'm no expert, but my quick test seems to suggest that user defined
properties of the window become undefined when the document changes.
However, the built-in properties do not. So, say the original window
document source looked like

<script type='text/javascript'>
function openIt(){
opener = open('child.htm','child', 'height=200,width=200');
}
</script>
<title>A</title>
<a href='b.htm'>b</a>
<br>
<button onclick='openIt();'>openIt()</button>

And the child window document:

<title>Child</title>
Child window

And the second document:

<script type='text/javascript'>
function closeIt(){
opener.close();
}
</script>
<title>B</title>
<button onclick='closeIt();'>closeIt()</button>

My test (IE6) shows that the value of the opener property survives the
document change, so the closeIt() function created by the second
document closes the child window.

As I said, I'm strictly a dabbler, so FWIW.
 
D

Davide Bruzzone

I'm no expert, but my quick test seems to suggest that user defined
properties of the window become undefined when the document changes.
However, the built-in properties do not. So, say the original window
document source looked like

<script type='text/javascript'>
function openIt(){
opener = open('child.htm','child', 'height=200,width=200');
}
</script>
<title>A</title>
<a href='b.htm'>b</a>
<br>
<button onclick='openIt();'>openIt()</button>

And the child window document:

<title>Child</title>
Child window

And the second document:

<script type='text/javascript'>
function closeIt(){
opener.close();
}
</script>
<title>B</title>
<button onclick='closeIt();'>closeIt()</button>

My test (IE6) shows that the value of the opener property survives the
document change, so the closeIt() function created by the second
document closes the child window.

As I said, I'm strictly a dabbler, so FWIW.

That's exactly what I was looking for! Thank you!

Dave
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top