2011-08-30 210 views
0
<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> 
+0

可能重複http://stackoverflow.com/questions/4068373/center-a-popup-window在屏幕上) – epascarello

回答

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) 

} 
[?中心屏幕上的彈出窗口(的
0

您指定頂部和您的參數列表中不止一次left值

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()} 
} 
+0

Oooops:p仍然無法正常工作? – Cameron

+0

嘗試在'window.open'調用之前用'console.log(centeredX);'或'alert(centeredX)'檢查'centeredX'和'centeredY'值。在Chrome上,它給我沒有定義。 – Fabio

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

爲什麼你有它兩次?

此外,代碼不能跨瀏覽器友好。

+0

對於跨瀏覽器兼容性問題+1。 – Fabio