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

时间:2013-05-31 15:37:26

标签: javascript

我在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打开新窗口(文件zoom.html)并将其调整为图像的X和Y(url varriable)。但问题是屏幕右上角会出现一个新窗口,也会出现在屏幕外面。如何在屏幕中间显示窗口?

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题!

{
    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);
}