Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to reference main window 1

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
US
on my main page I have:

window.name="mainw";

from another window, not necessary a child window, I am trying to refresh main window with:

mainw.location.reload();

and I am getting error that mainw doesn't exist.

what's wrong here?

Thanks
 
I believe there has to be an inherent relationship between the two windows. A window can only reference a window that it has created or a window that has created it.

I'd be interested if there is another method.

ToddWW
 
It is very possible for any arbitrary window to grab a handle to any other arbitrary window, assuming that the name of the window is known.

Take this, for example. Save the following as "mainw.html":

Code:
<html>
<head></head>
<body>
	Content for mainw <script type="text/javascript">document.write(new Date());</script>
</body>
</html>

And the following as both "winOpener1.html" and "winOpener2.html":

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		var winHandle = null;

		function openMain() {
			winHandle = window.open('mainw.html', 'mainw', '');
		}

		function reloadMain() {
			if (winHandle != null) winHandle.location.reload(true);
		}
	//-->
	</script>
</head>
<body>
	<a href="javascript:openMain();">Open window called 'mainw'</a>
	<br />
	<a href="javascript:reloadMain();">Reload window called 'mainw'</a>
</body>
</html>

If you run both the winOpener files in different windows, and then using winOpener1, choose to open the window, you can reload it as you please using the reload link.

If in winOpener2.html, you try using reload (before using the open link), nothing happens. As soon as you use the open link, it tries to open a window with the same name as one already open (even by another process), and gets a handle to the pre-existing one instead. From this point onwards, both winOpener1 and winOpener2 have a handle to the same window - and can both reload it - even though it was not originally opened by winOpener2.

Hope this helps!

Dan
 

P.S. This works in Firefox as well as IE... But will (although I've not tested this) probably only ever work as long as all windows reference files in the same domain.

Dan
 
Iin the case you presented
mainw.html is child of winOpener.html.

You have open mainw.html from within winOpener and you assigned name to it window.open 'mainw.html', 'mainw', ''). This is not what I was asking for.

Let's look at Billy's example:
My question is how to reference winOpener.html from within mainw.html. I don't want to use opener.location.reload(). I want to assign name to main window, in this/your case WinOpener.html, and later on from within mainw.html do something like: winopener.location.reload()

I found that you can assign name to any window with window.name = "mywinname" , but I can't reference this window afterwards. I always get message that object is not defined or so....

thanks...
 
bobetko,
You should try Dan's example. It'll work. That is how you establish a connection to an existing window.

This doesn't open a new window if 'mainw' already exists, it just assigns the existing window to the variable winHandle:
Code:
var winHandle = window.open('mainw.html', 'mainw', '');

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Let me just clarify. After you do this:
Code:
window.name="mainw";
You'll be able to reference that window from any other window using this:
Code:
[green]// Establishes a connection[/green]
var mainw = window.open('', 'mainw');
[green]// Now you can use mainw to refer to the window[/green]
mainw.location.reload();

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 

Hmm... I think you totally missed my point. Maybe if I rename my files, and change the wording of some of my links, it might be easier for you to understand the process I was showing you.

Take this file, and call it "mainw.html":

Code:
<html>
<head>
    <script type="text/javascript">
    <!--
    	window.name = 'mainw';
    //-->
    </script>
</head>
<body>
    Content for mainw <script type="text/javascript">document.write(new Date());</script>
</body>
</html>

Then take this file and call it "winUpdater.html":

Code:
<html>
<head>
    <script type="text/javascript">
    <!--
        var winHandle = null;

        function openMain() {
            winHandle = window.open('mainw.html', 'mainw', '');
        }

        function reloadMain() {
            if (winHandle != null) winHandle.location.reload(true);
        }
    //-->
    </script>
</head>
<body>
    <a href="javascript:openMain();">Get handle to window called 'mainw'</a>
    <br />
    <a href="javascript:reloadMain();">Reload window called 'mainw'</a>
</body>
</html>

Run both in separate windows. Note that neither open the other - they are not related in any way.

Now, in winUpdater.html, click the "Get handle to window called 'mainw'" link, and then you can update it anytime you like.

The idea behiind my previous post was to show you how any page could grab a handle to any other page, but I guess the whole window.open thing threw you?

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top