Skip to content

Instantly share code, notes, and snippets.

@mcraz
Created January 3, 2014 05:56
Show Gist options
  • Save mcraz/8233539 to your computer and use it in GitHub Desktop.
Save mcraz/8233539 to your computer and use it in GitHub Desktop.
Create & Center Popup Windows (Also on dual monitor)
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;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
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=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
}
@Torvin
Copy link

Torvin commented Oct 14, 2020

// Fixes dual-screen position                         Most browsers      Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;

This can't be right. window.screenLeft returns the X coordinate of the window, while screen.left returns the X coordinate of the leftmost screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment