homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

JS q - window.open positioning; also, window closing
Question about how to position a popup window and how to close it

10+ Year Member

Msg#: 182 posted 6:57 pm on Aug 27, 2002 (gmt 0)

Am wondering which is the correct way to position a popup window in the upper left-hand corner... is it

Also, what would the script be for closing the window with a text link?



10+ Year Member

Msg#: 182 posted 7:27 pm on Aug 27, 2002 (gmt 0)

It is different in IE and Netscape, so declare both (cut into a blank html document for an example):

<script type="text/javascript">
var window_name = window.open('foo.htm','window_name','[b]screenX=0,screenY=0,top=0,left=0[/b]');
<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>

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]


10+ Year Member

Msg#: 182 posted 7:37 pm on Aug 27, 2002 (gmt 0)

thanks a lot!
is there a reason to use

<script type="text/javascript">
var window_name = window.open('foo.htm','window_name','screenX=0,screenY=0,top=0,left=0');

instead of

<a href="#" onClick="window.open('foo.htm','screenX=0,screenY=0,top=0,left=0'); return false ;">
I've been using the latter up till now.


10+ Year Member

Msg#: 182 posted 7:41 pm on Aug 27, 2002 (gmt 0)

Just code portability. If you want to put the window open into a function, you can call it from a lot of different links on the page and just pass it arguments in the function call. So you could have:

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.

Plus, you'll need to put the window into a variable to get the window.close() method to work from another window.


WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 182 posted 7:48 pm on Aug 27, 2002 (gmt 0)

as has been mentioned around a few times use

<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.


10+ Year Member

Msg#: 182 posted 10:10 pm on Aug 27, 2002 (gmt 0)

jatar_k makes a good point.

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.


10+ Year Member

Msg#: 182 posted 7:24 am on Aug 28, 2002 (gmt 0)

I always wonder about screenX/screenY - I never use it and get the same relative to the parent position of a new window in IE and NN4 with top/left.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved