javascript Window.popup 在浏览器中居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18977246/
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-10-27 13:55:53  来源:igfitidea点击:

Window.popup in center align in browser

javascriptjquery

提问by Javascript Coder

I have a Div data which is coming dynamically. I want to open that div data in popup after some event fired. I am done with my code. But when popup is opening in my browser its not centered aligned.. Its coming in corner..

我有一个动态的 Div 数据。我想在触发某些事件后在弹出窗口中打开该 div 数据。我完成了我的代码。但是当弹出窗口在我的浏览器中打开时它没有居中对齐..它在角落里..

My code :-

我的代码:-

        var w  = 620;
        var h = 360;
        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        var divText = document.getElementById("inviteConfirmationMessage").outerHTML;
        var myWindow = window.open('','','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
        var doc = myWindow.document;
        doc.open();
        doc.write(divText);
        doc.close();

Working in Mozilla firefox but not working in Chrome --

在 Mozilla firefox 中工作但在 Chrome 中不工作——

http://jsfiddle.net/Wj3Qk/2/

http://jsfiddle.net/Wj3Qk/2/

Please help me out !! or any guidance.

请帮帮我!!或任何指导。

回答by Ankit Agrawal

    var left = (window.screen.width / 2) - ((w / 2) + 10);
    var top = (window.screen.height / 2) - ((h / 2) + 50);

window.open('','',
    "status=no,height=" + height + ",width=" + width + ",resizable=yes,left="
    + left + ",top=" + top + ",screenX=" + left + ",screenY="
    + top + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");

回答by Ganesh Pandhere

var myWindow = window.open('','','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

you are missing last quote in it.

您缺少其中的最后一个引号。

Is should be like this :

应该是这样的:

var myWindow = window.open('','','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left+');