[펌글] javascript window.open() 으로 팝업 호출시 팝업이 화면의 중앙에 뜨도록(screen center align) 하는 방법 (듀얼모니터 고려)

By | 2월 28, 2017

아직 완전히 검증한 것은 아니고 그냥 코드만 퍼 옴.

- 출처: http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen

 

function PopupCenter(url, title, w, h) {
    // Fixes dual-screen position                         Most browsers      Firefox
    var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
    var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
    var left = ((width / 2) - (w / 2)) + dualScreenLeft;
    var top = ((height / 2) - (h / 2)) + dualScreenTop;
    var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
    // Puts focus on the newWindow
    if (window.focus) {
        newWindow.focus();
    }
}

 

듀얼모니터에서 테스트 해 보니 그럭저럭 검증 됨~^ㅁ^

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments