Center a popup window on screen?

前端 未结 18 1129
一整个雨季
一整个雨季 2020-11-22 16:51

How can we center a popup window opened via javascript window.open function on the center of screen variable to the currently selected screen resolution ?

18条回答
  •  时光说笑
    2020-11-22 17:21

    If you want to center it on the frame you are currently in, I would recommend this function:

    function popupwindow(url, title, w, h) {
        var y = window.outerHeight / 2 + window.screenY - ( h / 2)
        var x = window.outerWidth / 2 + window.screenX - ( w / 2)
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + y + ', left=' + x);
    } 
    

    Similar to Crazy Tim's answer, but doesn't use window.top. This way, it will work even if the window is embedded in an iframe from a different domain.

提交回复
热议问题