在屏幕/浏览器的中心显示弹出窗口

时间:2011-08-30 11:56:28

标签: javascript

<html>
<head>
<script language="javascript" type="text/javascript" >

function popupwindow(url, title, w, h)
{
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var new_left = window.screenX + (((window.outerWidth/2) - (w/2)));
    var new_top = window.screenY + (((window.outerHeight/2) - (w/2)));

    return window.open(url, title, 'width='+w+', height='+h+', top='+new_top+', left='+new_left+',toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no');
}

popupwindow('index.html','','1024','768');


</script>
</head>
<body>

</body>
<html>

3 个答案:

答案 0 :(得分:1)

看看你发布的内容

newwindow = window.open('index.html','_blank', 'top=0,left=0,width=1024,height=768,top='+centeredY+',left='+centeredX+'');

您有top=0,left=0top='+centeredY+',left='+centeredX

你为什么要两次?

此外,代码不是跨浏览器友好的。

答案 1 :(得分:0)

您在参数列表中多次指定了顶部和左侧值

function popup()
{
    newwindow = window.open('index.html','_blank', 'top=0,left=0,width=1024,height=768,top='+centeredY+',left='+centeredX+'');
                                                    ^^^^^^^^^^^^^
    if (window.focus) {newwindow.focus()}
}

答案 2 :(得分:0)

您可以使用以下代码执行此操作:

function centerWindow() {
leftPos = 0
topPos = 0
if (screen) {
leftPos = (screen.width / 2) - 251
topPos = (screen.height / 2) - 162
}
ElementWindow = window.open('index.html','_blank',
'width=502,height=325,left='+leftPos+',top='+topPos)

}