PDA

View Full Version : Opening new browser window, centered




tech4all
Feb 3, 2005, 09:23 PM
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 :)



mnkeybsness
Feb 3, 2005, 09:31 PM
You need a javascript to do that... but please DON'T! It really bothers people when you mess with their browser windows and sizes.

angelneo
Feb 3, 2005, 09:46 PM
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
Feb 3, 2005, 09:46 PM
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
Feb 3, 2005, 09:48 PM
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
Feb 3, 2005, 09:57 PM
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
Feb 3, 2005, 11:14 PM
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
Feb 4, 2005, 03:28 AM
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
Feb 4, 2005, 11:48 PM
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
Feb 7, 2005, 01:39 PM
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
Feb 7, 2005, 09:44 PM
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


<!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
Jul 8, 2005, 08:00 PM
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
Jul 9, 2005, 02:14 AM
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>