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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open new window center screen

Status
Not open for further replies.

robman22

Programmer
Nov 28, 2000
12
CA
I'm wondering if there is a script that I can use where
a window will open up in the center of a user's screen
no matter what resolution or screen size they have.

Thanks
 
Try this:

<html>
<head>
<script language=&quot;Javascript&quot;>
// set the size of your window
var window_width = 500;
var window_height = 500;

function move_window(){
//resize the window
window.resizeTo(window_width,window_height);
//reposition the window
window.moveTo((screen.width-window_width)/2,(screen.height-window_height)/2);
}
</script>
</head>
<body onLoad=&quot;move_window();&quot;>
Hello there!!!
<body>
</html>


screen.width gets the screen width, and (screen.width-window_width)/2 sets the distance the window should be from the left.
Similarly for height.

NOTE that you are difining the height and width of the window, instead of finding what the current height and width are.

Simon
 
Javascript 1.2 enables you to set where the window pops up, this is good because using moveTo on a slow connection looks a little budget. The only problem with this theory is that I have yet to get the bastard to work myself! So if you know how to get the browser to read javascript1.2 then you can use these attributes to pop it up wherever you like:


screenX
screenY

Also (supposedly) width and height have been replaced by innerHeight/Width...

I am really confused about versions, so if you know, please tell me!

T.I.A, Ben
 
OK, try this:

<html>
<head>
<script language=&quot;Javascript&quot;>
// set the size of your new window
var window_width = 500;
var window_height = 500;

function open_window(){
var left = (screen.width-window_width)/2;
var top = (screen.height-window_height)/2;
msg=window.open(&quot;newpage.html&quot;,&quot;msg&quot;,&quot;height=&quot;+window_height+&quot;,width=&quot;+window_width+&quot;,left=&quot;+left+&quot;,top=&quot;+top);
}

</script>
</head>
<body>
Hello there!!!<BR>
<input type=&quot;Button&quot; onclick=&quot;open_window();&quot; value=&quot;New Window&quot;>
<body>
</html>

Simon
 
Actually, I have found this before, this weird thing where sometimes pixelTop/Left work sometimes but not others. This happens when I try to move elements and acces their positioning thru a cross browser DOM.

Is this one of those times you think? I am curious about those &quot;+&quot; signs. They are in a pattern matching context are they? - Is this like using eval(), but different, you know since you can't pass references into quotes, I have never used these before.

I'll give top a go! thanks, ben
 
Yes that's right. all this time I should have been using top and left instead of pixelTop!

I can't believe this! Why the heck would this book use pixelTop?
 
Thanks Simon, but I need scroll bars and when I place them
in the script, the placement goes out of wack.

Any ideas?
 
By the way way, joining strings, yes I know! Long day...

O.K. do you think you can just allow for it, the scrollbars I mean? Could you clarify how out of wack it gets?

Thanks, ben. &quot;Lyin, cheatin, hu-urtin, that's a-all you seem to do-oo-oo&quot;
 
robman22,

When you say &quot;...when I place them [scroll bars] in the script, the placement goes out of whack.&quot; what do actually mean??

Simon
 
Add 20 to the height and width of the window to incorporate scrollbars.
Code:
var window_width = 500;
var window_height = 500;

function open_window()
{
  var left = (screen.width-(window_width+20))/2;
  var top = (screen.height-(window_height+20))/2;
  msg=window.open(&quot;newpage.html&quot;, &quot;msg&quot;, &quot;height=&quot;+window_height+&quot;,width=&quot;+window_width+&quot;,left=&quot;+left+&quot;,top=&quot;+top+&quot;,scrollbars=yes&quot;);
}

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Sorry for the unclear explanations guys.
Thanks for your help, that's perfect Tom.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top