Forum Moderators: open
<script type="text/javascript">
var window_name = window.open('foo.htm','window_name','[b]screenX=0,screenY=0,top=0,left=0[/b]');
</script>
<p>
<a href="javascript: window_name.close();">Close that window</a><br />
or if you want the link to go to foo.htm:<br />
<a href="foo.htm" onclick="window_name.close();">Close that window</a>
</p> More info at MSDN [msdn.microsoft.com] and at Netscape's documentation site [developer.netscape.com].
[edited by: moonbiter at 7:38 pm (utc) on Aug. 27, 2002]
<script type="text/javascript">
var window_name = window.open('foo.htm','window_name','screenX=0,screenY=0,top=0,left=0');
</script>
instead of
<a href="#" onClick="window.open('foo.htm','screenX=0,screenY=0,top=0,left=0'); return false ;">
Foo</a>
?
I've been using the latter up till now.
function openWindow(url) {
var window_name = window.open(url,'window_name','screenX=0,screenY=0,top=0,left=0');
} and call it like:
<a href="#" onclick="openWindow('foo.htm')">Foo link</a>
<a href="#" onclick="openWindow('foo2.htm')">Foo link 2</a>
<a href="#" onclick="openWindow('foo3.htm')">Foo link 3</a> and so on.
<added>
Plus, you'll need to put the window into a variable to get the window.close() method to work from another window.
</added>
<a href="javascript:openWindow('foo.htm');">Foo link</a>
instead of
<a href="#" onclick="openWindow('foo.htm')">Foo link</a>
This will keep the page from jumping around and leave the back button able to be used on a single click instead of the user having to click twice to go back.
Another alternative is:
<a href="foo.htm" onclick="openWindow('foo.htm'); return false">Close that window</a> Which has the added benefit of failing gracefully, allowing those who have javascript turned off to visit the url too (it will show up in the main window). Plus, if you want to get a little clever make your function base itself off of the href of the link, so that you could do:
function openWindow(obj) {
var url = obj.href;
var window_name = window.open(url,'','screenX=0,screenY=0,top=0,left=0');
}
And call it with
<a href="foo.htm" onclick="openWindow(this); return false;">Open that window</a>. This way, you never have to change the arguments in the function call, and just changing the href url is enough.