Opening new browser window, centered

tech4all

macrumors 68040
Original poster
Jun 13, 2004
3,400
489
NorCal
What I want to do: Click a link, then open new window (at predetermined size) in center of screen.

Problem: New window does not open in center of screen.

Is there some way to open a new browser window so that it is centered? I am using Dreamweaver and have a predetermined size for the new browser window. But it would be nicer to have it open centered on the screen.

I currently select the link where people would click, then go to Behaviors > + > Open Browser Window, to do this. But having it centered would be more appealing.

Any help is appreciated :)
 

angelneo

macrumors 68000
Jun 13, 2004
1,541
0
afk
just do a:

your_window_variable.moveTo((screen.width-your_window_width)/2,(screen.height-your_window_height)/2)

have not test it yet.

and yes I agree that moving windows sometimes irritate my surfing
 

tech4all

macrumors 68040
Original poster
Jun 13, 2004
3,400
489
NorCal
mnkeybsness said:
You need a javascript to do that... but please DON'T! It really bothers people when you mess with their browser windows and sizes.
Well for now I'm gonna keep like that. If I get complaints about it, then I'll change, but the design kinda needs a new window to allow for cropping. But I will keep your suggestion in mind.

So do you know the script? :)
 

tech4all

macrumors 68040
Original poster
Jun 13, 2004
3,400
489
NorCal
angelneo said:
just do a:

your_window_variable.moveTo((screen.width-your_window_width)/2,(screen.heigh-your_window_height)t/2)

have not test it yet.

and yes I agree that moving windows sometimes irritate my surfing

Thanks. But what do you mean by "moving windows"? Like switching windows or something?
 

angelneo

macrumors 68000
Jun 13, 2004
1,541
0
afk
tech4all said:
Thanks. But what do you mean by "moving windows"? Like switching windows or something?
The javascript would create the window and then execute the positioning, this might mean that for slower computer, they would see the windows pop up in the default position and then moved to the new one. For faster computer shouldn't be too much of a problem.

EDIT: tested it on safari and explorer, should be no problem. plus I made a typo, the extra "t" should go to the height.... i amended my original post.
 

tech4all

macrumors 68040
Original poster
Jun 13, 2004
3,400
489
NorCal
angelneo said:
The javascript would create the window and then execute the positioning, this might mean that for slower computer, they would see the windows pop up in the default position and then moved to the new one. For faster computer shouldn't be too much of a problem.

EDIT: tested it on safari and explorer, should be no problem. plus I made a typo, the extra "t" should go to the height.... i amended my original post.
Thanks, doing a test on it right now. Question; where abouts in the code do I put it at?
 

angelneo

macrumors 68000
Jun 13, 2004
1,541
0
afk
tech4all said:
Thanks, doing a test on it right now. Question; where abouts in the code do I put it at?
you are using dreamweaver right? the code will goes right after you pop up the window (I think its MM_openBrWindow(....)), the "your_window_variable" is the window name you have given to that particular window.
 

tech4all

macrumors 68040
Original poster
Jun 13, 2004
3,400
489
NorCal
angelneo said:
you are using dreamweaver right? the code will goes right after you pop up the window (I think its MM_openBrWindow(....)), the "your_window_variable" is the window name you have given to that particular window.
Thank you for your reply.

I having some trouble figuring out where it goes in the code. I tried what you said, but it doesn't appear to be working. I can't seem to find the right place to put it in or am not labeling things right. Could you (or anyone) possibly put the code as it would appear in the HTML? The name of the file that is supposed to pop up is called "TEST2" and the name of the window is also, "TEST2". The dimensions are 100x100 px. I'm just trying to do a test right now. You can use the "TEST2" name for an example when putting it into the code.

Thanks :)
 

HeWhoSpitsFire

macrumors member
Aug 13, 2004
51
0
Oregon
You must be creating redundant nested <script> tags in your <head>

You can actually parse javascript virtually anywhere inside <html> tag if I recall right.
 

angelneo

macrumors 68000
Jun 13, 2004
1,541
0
afk
tech4all said:
Thank you for your reply.

I having some trouble figuring out where it goes in the code. I tried what you said, but it doesn't appear to be working. I can't seem to find the right place to put it in or am not labeling things right. Could you (or anyone) possibly put the code as it would appear in the HTML? The name of the file that is supposed to pop up is called "TEST2" and the name of the window is also, "TEST2". The dimensions are 100x100 px. I'm just trying to do a test right now. You can use the "TEST2" name for an example when putting it into the code.

Thanks :)
I'm so sorry that I missed your post. Here's a sample page. save it as testing.html

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  testwin = window.open(theURL,winName,features);
}
//-->
</script>
</head>

<body>
<a href="#" onClick="MM_openBrWindow('testing.html','testwin','width=400,height=400');testwin.moveTo((screen.width-400)/2,(screen.height-400)/2);">testing</a><br>
</body>
</html>
 

mypointofview

macrumors newbie
Jul 8, 2005
5
0
Boulevard Barbes
There's another possibiliy that avoids "move"

Here's another way to center a popup which avoides the move which can indeed be annoying.

Here's what I've come up with (example for a 783 x 397 pixel window). I called the function "makeWin1" because (unfortunately) I could not see any other possibility than this, which requires to write the link in the JavaScript header AND in the normal code.

--
The Javascript in the header:

<script type="text/javascript" language="JavaScript">
<!--

// POPUP_1

function makeWin1(url,width,height) {
x = (width)/2, y = (height)/2;

if (screen) {
y = (screen.availHeight - height)/2;
x = (screen.availWidth - width)/2;
}
window.open('http://www.mysite.com/mypage.html','newWin','width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x);
}


//-->
</script>

--
Then in the text part:

Click here for <a href="http://www.mysite.com/mypage.html" onClick="makeWin1('',783,397); return false">pop-up.</a>

--

The only problem that I'm having is that the window is resizable in Safari! My question is therefore, how can I prevent this?

Earlier I had used a different code that could not center the pop-up window in Explorer (but centered in Safari) but it did not allow resizing it in Safari. I tried a combination of both codes but it did not help (I added "resizable=0").

I'm not a professional webmaster. I'm using code that I find on pages which I like. Can somebody help me - somebody who might be familiar with Safari?

How do I avoid that a user can resizize this window in Safari?

Thanks in advance, Martin

MacOS 10.39 Panther
 

mypointofview

macrumors newbie
Jul 8, 2005
5
0
Boulevard Barbes
Cross platform popup that centers

OK, I think I found the perfect cross platform solution for centered popup windows. I found it on the site of Panic.com, I hope Cabel won't mind ;-)

The trick for all problems is to have a JavaScript that distinguishes between different platforms.

Here's the sample page which will bring up - upon click on a link - a pop up of 500 by 300 pixels. I used as link www.apple.com -- just an example.

To have scroll bars, resizability, etc work, simply change the digit "0" to "1" in all 3 ocurrences.



<html>
<head>

<script type="text/javascript" language="JavaScript">
<!--

// POPUP

var isNav4, isIE4, isMac;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
isNav4 = (navigator.appName == "Netscape") ? true : false
isNav6 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 5) ? true : false
isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false
isMac = (navigator.platform.indexOf("Mac") != -1) ? true : false
}


function popup(url, winWidth, winHeight) {
if (isNav4 || isIE4) {
var screenPosX,screenPosY;
screenPosX = (screen.availWidth - winWidth) / 2;
screenPosY = (screen.availHeight - winHeight) / 2;

if (isNav4 || isNav6) {
window.open(url, 'subwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0, copyhistory=0,width='+winWidth+',height='+winHeight+',screenX='+screenPosX+',screenY='+screenPosY+'');
}

if (isIE4) {
newwin = window.open(url, 'subwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0, copyhistory=0,width='+winWidth+',height='+winHeight+',left='+screenPosX+',top='+screenPosY+'');
}
}

else {
window.open(url, 'subwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0, copyhistory=0,width='+winWidth+',height='+winHeight+'');
}
}


//-->
</script>


<a href="http://www.apple.com/" onClick="popup('http://www.apple.com/','500','300'); return false;">Click here for pop-up</a>

</body>
</html>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.