窗口居中显示的方法:使用window.open。

14

@Rionmonster 那个问题的被接受答案是一个相当糟糕的脚本,而且还不完整。 - Pointy
@Pointy,也许你可以改进它并发布。或者,至少给亚历克斯一些关于它为什么糟糕的指导。 - Brad
你为什么只想在 onclick 中使用它?这样会很快变得难以阅读。 - FishBasketGordo
除非您能提供更好的替代方案,否则它必须能够在同一页中多次使用,但宽度、高度和位置将始终保持不变。 - manc
2
可能是如何在屏幕中央显示弹出窗口?的重复问题。 - AMIC MING
显示剩余2条评论
4个回答

32

编辑

这是一个糟糕的答案。可以在这里找到一个更好的答案: window.open() 在多显示器/双显示器系统中弹出窗口的位置

但与此同时,当我决定何时更新此答案时,这个示例可考虑双显示器设置: http://jsfiddle.net/w665x/138/

原始回答

这可能适用于您。不能完全保证跨浏览器兼容性,但接近;

<html>
<head>
<script type="text/javascript">
function goclicky(meh)
{
    var x = screen.width/2 - 700/2;
    var y = screen.height/2 - 450/2;
    window.open(meh.href, 'sharegplus','height=485,width=700,left='+x+',top='+y);
}
</script>
</head>
<body>
<a href="http://path/to/url" onclick="goclicky(this); return false;" target="_blank">blah</a>
</body>
</html>

点这里!


1

此外,您还可以尝试:

$('a.popup-window').on('click', function(){
    var w = 880, h = 600,
        left = Number((screen.width/2)-(w/2)), tops = Number((screen.height/2)-(h/2)),
        popupWindow = window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=1, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
    popupWindow.focus(); return false;
});

并调用:

<a href="https://google.com/" class="popup-window">Open in new window</a>

0

这段代码完美运行:

//Code Starts
$(document).ready(function() {
   $('#Popup').click(function() {
     var NWin = window.open($(this).prop('href'), '', 'height=800,width=800');
     if (window.focus) 
     {
       NWin.focus();
     }
     return false;
    });
});​
//Code Ends


<a href="http://www.google.com/" id="Popup">Open in Popup window</a>

在新标签页或弹出窗口中打开链接 - S. S.
你能提供一些关于它如何工作的解释吗?除了包含链接之外。 - mdml

0

这段代码使用了jAplus脚本。它可以让您在不编写JavaScript代码的情况下实现此功能。(http://japlus.simplit.it)

<head>
   <script src="/path/to/jquery.js" type="text/javascript" charset="utf-8"></script>
   <script src="/path/to/jquery.Aplus.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <a href="http://path/to/url" class="win win-center">
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接