Skip to content

Instantly share code, notes, and snippets.

@nicholasxjy
Created September 20, 2018 10:13
Show Gist options
  • Save nicholasxjy/aa697aaff825b91d852e554e275d7915 to your computer and use it in GitHub Desktop.
Save nicholasxjy/aa697aaff825b91d852e554e275d7915 to your computer and use it in GitHub Desktop.
open a popup window in center
export function popupUrlCenterWindow(url, title, w, h) {
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 left = width / 2 - w / 2 + dualScreenLeft
const top = height / 2 - h / 2 + dualScreenTop
const 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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment