Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a popup window on screen?

Tags:

javascript

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 ?

like image 483
Zo Has Avatar asked Nov 01 '10 11:11

Zo Has


People also ask

How do I center a pop up modal?

In this example first, use the find() method to find out the modal dialog. Then subtract the modal height from the window height and divide that into half and will place the modal that will be the centered(vertically). This solution will dynamically adjust the alignment of the modal.

How do I center my browser window?

Press Alt + Space . Press M (for “Move”). Use the arrow keys to move the window exactly where you want it.

How do you keep a pop up window always on top?

To make the active window always on top, press Ctrl + Spacebar (or the keyboard shortcut you assigned).


2 Answers

SINGLE/DUAL MONITOR FUNCTION (credit to http://www.xtf.dk - thank you!)

UPDATE: It will also work on windows that aren't maxed out to the screen's width and height now thanks to @Frost!

If you're on dual monitor, the window will center horizontally, but not vertically... use this function to account for that.

const popupCenter = ({url, title, w, h}) => {     // Fixes dual-screen position                             Most browsers      Firefox     const dualScreenLeft = window.screenLeft !==  undefined ? window.screenLeft : window.screenX;     const dualScreenTop = window.screenTop !==  undefined   ? window.screenTop  : window.screenY;      const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;     const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;      const systemZoom = width / window.screen.availWidth;     const left = (width - w) / 2 / systemZoom + dualScreenLeft     const top = (height - h) / 2 / systemZoom + dualScreenTop     const newWindow = window.open(url, title,        `       scrollbars=yes,       width=${w / systemZoom},        height=${h / systemZoom},        top=${top},        left=${left}       `     )      if (window.focus) newWindow.focus(); } 

Usage Example:

popupCenter({url: 'http://www.xtf.dk', title: 'xtf', w: 900, h: 500});   

CREDIT GOES TO: http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html (I wanted to just link out to this page but just in case this website goes down the code is here on SO, cheers!)

like image 193
Tony M Avatar answered Oct 13 '22 23:10

Tony M


try it like this:

function popupwindow(url, title, w, h) {   var left = (screen.width/2)-(w/2);   var top = (screen.height/2)-(h/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='+top+', left='+left); }  
like image 36
oezi Avatar answered Oct 14 '22 00:10

oezi