Skip to content

Instantly share code, notes, and snippets.

@himalay
Last active August 30, 2017 10:19
Show Gist options
  • Save himalay/5dd37180bc67adf8d0cfec871642f883 to your computer and use it in GitHub Desktop.
Save himalay/5dd37180bc67adf8d0cfec871642f883 to your computer and use it in GitHub Desktop.
Open centered popup window
// open with html string
function openPopupWin (content, width, height) {
var w = innerWidth || document.documentElement.clientWidth || screen.width
var h = innerHeight || document.documentElement.clientHeight || screen.height
var position = 'scrollbars=yes,width=' + width + ',height=' + height
position += ',top=' + ((h - height) / 2 + screenTop || screen.top || 0)
position += ',left=' + ((w - width) / 2 + screenLeft || screen.left || 0)
var popupWin = window.open('about:blank', '', position)
popupWin.document.write(content)
if (window.focus) popupWin.focus()
}
// open url with a title
function openPopupWin (url, title, width, height) {
var w = innerWidth || document.documentElement.clientWidth || screen.width
var h = innerHeight || document.documentElement.clientHeight || screen.height
var position = 'scrollbars=yes,width=' + width + ',height=' + height
position += ',top=' + ((h - height) / 2 + screenTop || screen.top || 0)
position += ',left=' + ((w - width) / 2 + screenLeft || screen.left || 0)
var popupWin = open(url, title, position)
if (focus) popupWin.focus()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment