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 ?
My version with ES6 JavaScript.
Works well on Chrome and Chromium with dual screen setup.
function openCenteredWindow({url, width, height}) {
const pos = {
x: (screen.width / 2) - (width / 2),
y: (screen.height/2) - (height / 2)
};
const features = `width=${width} height=${height} left=${pos.x} top=${pos.y}`;
return window.open(url, '_blank', features);
}
Example
openCenteredWindow({
url: 'https://stackoverflow.com/',
width: 500,
height: 600
}).focus();