Center Modal Window window.open with JavaScript

1

I would like to know how to center a modal window made with window.open horizontally and vertically, what I have is this:

 var myWindow ="wa.me/…?"; 
 window.open(myWindow, "", "width=600,height=400");
    
asked by Noe Muñoz 18.10.2018 в 05:42
source

1 answer

1

Try the following:

<script>
function PopupCenter(url, title, w, h) {
    // Fixes dual-screen position                         Most browsers      Firefox
    var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : window.screenX;
    var dualScreenTop = window.screenTop != undefined ? window.screenTop : window.screenY;

    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();
    }
}
</script>
<button type="button" onclick="PopupCenter('http://www.google.com.ar','xtf','600','400');">Open</button>

Please read how to ask . To be able to formulate your questions correctly in the future. I hope you get the answer. Greetings!

    
answered by 18.10.2018 / 06:14
source