Center a popup window on screen?

前端 未结 18 1034
一整个雨季
一整个雨季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 17:20

    function fnPopUpWindow(pageId) {
         popupwindow("hellowWorld.php?id="+pageId, "printViewer", "500", "300");
    }
    
    function popupwindow(url, title, w, h) {
        var left = Math.round((screen.width/2)-(w/2));
        var top = Math.round((screen.height/2)-(h/2));
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, '
                + 'menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w 
                + ', height=' + h + ', top=' + top + ', left=' + left);
    }
    
    Print Me
    

    Note: you have to use Math.round for getting the exact integer of width and height.

提交回复
热议问题