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

Centering a webpage 5

Status
Not open for further replies.

YellowOnline

Technical User
Mar 31, 2004
144
BE
Hi,

"Welcome to my first post" ;)

To get to the point: I created a webpage with, on one page, a link to a picture gallery opening in a new window.

Code:
Click <a href='javascript:void(0)' onClick="window.open('../_resources/gallery/index.html','NewWindow','width=650,height=450');">here</a> to enter our image gallery

Now I was wondering if there's a way to center my NewWindow on the screen. Now it just opens where the browser would regulary open (except that its dimensions are limited of course). Thanks in advance!

Peace,

Yellow
 
Welcome.

You can do this with JavaScript.
Though my previous efforts have never worked to my satisfaction.

Try over in the JavaScript forum, I am sure someone there could help out.
 
As Foamcow states, it can be done in javascript. What you have to do is create a function that determines the users screen width (and height) and then deducts the size of your window from these values. Divide them by 2 and you'll have the x,y or top,bottom values.

There's always a better way. The fun is trying to find it!
 
Not possible from HTML (no syntax for that) or CSS (window node has no styledefs).
Here is code anyway... for all other questions we should switch to javascript forum:

Code:
<a href="javascript:void(0);" onclick= "openWindow( 'blah.htm', 'PopUp', 300, 400, true, '' );">Click here</a>
...
<script language="javascript">
function openWindow( sURL, sName, iWidth, iHeight, bCenter, sExtra )
{	sArgs = "height=" + iHeight + ",width=" + iWidth;
	if (bCenter)
	{	sArgs += ",left="+ Math.floor((screen.width-iWidth)/2);
		sArgs += ",top=" + Math.floor((screen.height-iHeight)/2);
	}
	if (sExtra) sArgs+= ","+sExtra;
	
	return window.open( sURL, sName, sArgs );
}
</script>
 
I just got this working on a site that I'm redesigning. I ran across this code on another forum. Try this (it appears to be working ok for me):

Code:
var w = 800, h = 600;

if (document.all) 
{
        w = document.body.clientWidth;
	h = document.body.clientHeight;
}
else if (document.layers) 
{
	w = window.innerWidth;
	h = window.innerHeight;
}

var popW = 700, popH = 500;
var LeftPos = (w-popW)/2, TopPos = (h-popH)/2;

window.open ('print_screen.html', null, 'toolbar=no,height=500px,width=700px,scrollbars=yes,dependent=yes,Left=' + LeftPos + ',top=' + TopPos + ',alwaysRaised=yes');

Note: This will center the window according to the browser size. The clientWidth, clientHeight properties are only available after the onLoad.

Hope this helps,
- VB Rookie
 
Hello,

I was able to position my new window in an image map by using the follow code. You can adjust the "left" and "top" values to center your window on the page.

<area shape="rect" alt="" coords="160,281,293,305" href="newWindowURL" onClick="window.open('surfer.jpg','nameOfWindow','width=375,height=300,left=400,top=250');return false">
 
Thank you guys, the topic can be closed now. I'm new to the forum so I notice only now I'd better posted it in the JavaScript forum.
Vongrun's reply looks at first sight exactly what I need.

Peace,

Yellow
 
Yay, it worked indeed!

*** TOPIC CLOSED ***

Peace,

Yellow
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top