1

我正在尝试使用以下代码通过表单按钮启动居中的浏览器窗口...

http://www.bbc.co.uk','testwin','width=400, height=400, left=(screen.availWidth-400)/2, top=(screen.availHeight-400)/2' ); return false">测试按钮

新浏览器显示正确的高度和宽度,但 left 和 top 属性被忽略,因此窗口显示在左上角。

我是否过于雄心勃勃地试图使这段代码内联?我应该使用其他语法还是应该放弃并改为调用函数(如果可以避免的话,我宁愿不使用它)?

我使用的是 Firefox 3.0.17(最新),但在 IE7 中也会出现同样的效果。

TIA,艾伦·哈里斯-里德

4

1 回答 1

1

http://www.tek-tips.com/faqs.cfm?fid=2296解释了如何做到这一点。但由于弹出窗口拦截器,我建议使用浮动 Div。

假设您要打开一个大小为 200h x 150w 的窗口。通过取一半的高度和宽度(分别为 100 和 75),您可以确定窗口的中心。然后通过一些数学运算,您可以将窗口居中。这是如何完成的

<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>

要在 HTML 中调用此函数,请使用:

<A HREF="javascript:MyPopUpWin()">This is the link</a>
于 2010-02-13T04:08:34.033 回答