views:

26

answers:

1

Hi,

I am trying to launching a centered browser window via a form-button using the following code...

Test button

The new browser appears the correct height and width, but the left and top attributes are ignored, so the window appears top-left.

Am I being too ambitious trying to make this code in-line? Is there any other syntax I should use or should I give-up and call a function instead (I would rather not if I can avoid it)?

I am using Firefox 3.0.17 (latest), but the same effect happens in IE7.

TIA, Alan Harris-Reid

A: 

http://www.tek-tips.com/faqs.cfm?fid=2296 explains how to do it. But I would recomend using a floating Div instead because of pop-up blockers.

Lets say you want to open a window that has a size of 200h x 150w. By taking half of the height and width (100 and 75 respectively) you can pinpoint the center of the window. Then with a little math you can center the window. Here is how it is done

<SCRIPT LANGUAGE="JavaScript">
function MyPopUpWin() {
var iMyWidth;
var iMyHeight;
//half the screen width minus half the new window width (plus 5 pixel borders).
iMyWidth = (window.screen.width/2) - (75 + 10);
//half the screen height minus half the new window height (plus title and status bars).
iMyHeight = (window.screen.height/2) - (100 + 50);
//Open the window.
var win2 = window.open("filename.htm","Window2","status=no,height=200,width=150,resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
win2.focus();
}
</SCRIPT>

To call this function in HTML use this:

<A HREF="javascript:MyPopUpWin()">This is the link</a>
Todd Moses
That works nicely. Pity it didn't work as an inline onclick command, but I suppose functions are more flexibile in the long-run.Thanks,Alan
Alan Harris-Reid