Computing.Net > Forums > Web Development > Javascript- new window in center

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Javascript- new window in center

Reply to Message Icon

Name: ChromeShark (by croftstorm)
Date: March 6, 2005 at 09:57:10 Pacific
OS: xp home
CPU/Ram: -
Comment:

Hi. I want to use javascript to open a new window in the center of the screen:

<a onClick="javascript:window.open('myfile.htm', 'WinName',
'width=800,height=500,left=200,top=200');" Window target="_blank"><div align="center" class="style1">mytext</div>

As you see I have it set at 200px from the top left corner which is about center on my monitor, but is there a way of insuring that it will always be the center?

croftstorm.com



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: March 6, 2005 at 16:10:06 Pacific
Reply:

Find the resolution that the client computer is running (see link below) then instead of putting the window.open call into the href itself, you will need to create a function since you will need to programatically determine the left and top values.

function openWindow() {
leftVal = (800 - screen.width) / 2;
topVal = (500 - screen.height) / 2;

newWindow = window.open('myfile.htm', 'WinName',
'width=800,height=500,left='+leftVal+',top='+topVal;

}

That script may not work perfectly, but you get the idea.

Detecting screen resolution in JavaScript

Michael J


0

Response Number 2
Name: ChromeShark (by croftstorm)
Date: March 7, 2005 at 10:20:07 Pacific
Reply:

Hi again

after a bit of twiddling I got it working well, but there's a couple of problems. Here's the code so far:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="Javascript" type="text/javascript">

</script>
</head>

<body>
<div align="center">
<h3>Play Vermilion
</div>
</body>
</html>

The 2 problems:

1)The window opens up at the top center of the window, why?? I did the exact same thing for top and left values, so I dont see why left works and top doesnt.

2)Is it just me or does that window not look quite centered? I guess it doesnt matter much, but still...

croftstorm.com


0

Response Number 3
Name: ChromeShark (by croftstorm)
Date: March 7, 2005 at 10:22:28 Pacific
Reply:

Well that didnt work, I've uploaded the page to http://lizzlizz.com/test2.htm so please go look

croftstorm.com


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Javascript- new window in center

Need open image in new window JS www.computing.net/answers/webdevel/need-open-image-in-new-window-js/950.html

new window JS same as original www.computing.net/answers/webdevel/new-window-js-same-as-original/2907.html

VBScript new window www.computing.net/answers/webdevel/vbscript-new-window/1131.html