Javascript window.open 到屏幕中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6983552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:07:23  来源:igfitidea点击:

window.open to center of screen

javascriptpopupwindowwindow.open

提问by manc

I am using window.open to open a popup window like so:

我正在使用 window.open 打开一个弹出窗口,如下所示:

<a href="http://path/to/url" onclick="window.open(this.href, 'sharegplus', 'height=485,width=700'); return false;" target="_blank">

<a href="http://path/to/url" onclick="window.open(this.href, 'sharegplus', 'height=485,width=700'); return false;" target="_blank">

I want this to be centered in the screen, but without having to use <script> inline code </script>and merely enter whatever I need within onclick="". Can this be done?

我希望它以屏幕为中心,但不必使用<script> inline code </script>,只需输入我需要的任何内容onclick=""。这能做到吗?

回答by Joseph Marikle

Edit

编辑

This is a bad answer. a much better answer can be found here: window.open() on a multi-monitor/dual-monitor system - where does window pop up?

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

But in the meantime while i decide when i want to update this answer, this fiddle accounts for dual monitor setups: http://jsfiddle.net/w665x/138/

但与此同时,当我决定何时更新此答案时,此小提琴说明了双显示器设置:http: //jsfiddle.net/w665x/138/

Original Answer

原答案

This might work for you. Not confident in it being entirely cross-browser, but close;

这可能对你有用。对它完全跨浏览器没有信心,但很接近;

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

Fiddle!

小提琴!

回答by Tarampampam

Also you can try:

您也可以尝试:

$('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;
});

And call:

并调用:

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

回答by S. S.

This code works perfectly:

这段代码完美运行:

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

回答by erik

This code uses jAplusscript. It allows you to do this without writing JavaScript code. (http://japlus.simplit.it)

此代码使用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>