Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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

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

![]() |
![]() |
![]() |

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