如何用javascript在屏幕中间显示新窗口

How to display new window in the middle of the screen in javascript?

本文关键字:新窗口 窗口 显示 中间 何用 javascript 屏幕      更新时间:2023-09-26

我有代码在js:

<script>
function displayWindow(url, w, h)
{
  var win = window.open("zoom.htm","displayWindow",'width='+w+', height='+h+',resizable=yes,scrollbars=yes,menubar=no' );
  win.document.write("<html><body onclick=window.close()>");
  win.document.write("<img id=fullimg src=" + url+">");
  win.document.write("</body></html>");
  var image = win.document.getElementById("fullimg");
  win.resizeTo(image.width + 50, image.height + 50);
}
</SCRIPT>

Java Script打开新窗口(file zoom.html)并将其大小调整为图像(url变量)的X和Y。但问题是,新窗口显示在屏幕的右上角,也在屏幕外。如何在屏幕中间显示窗口?

应该可以了!

{
    var win = window.open(
            "zoom.htm", "displayWindow", 'width=' + w +
            ', height='+ h +', resizable=yes, scrollbars=yes, menubar=no');
    win.document.write("<html><body onclick='window.close();'>");
    win.document.write("<img id='fullimg' src=" + url + ">");
    win.document.write("</body></html>");
    var image = win.document.getElementById("fullimg");
    var width = image.width + 50;
    var height = image.height + 50;
    win.resizeTo(width, height);
    win.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
}