Skip to content

Instantly share code, notes, and snippets.

@fabio-filho
Created November 7, 2022 14:17
Show Gist options
  • Save fabio-filho/f3337412acaba0b2405688c1df0bdb78 to your computer and use it in GitHub Desktop.
Save fabio-filho/f3337412acaba0b2405688c1df0bdb78 to your computer and use it in GitHub Desktop.
Popup Window Centralized by Parent Window
<html>
<button onclick="popupCenter('https://google.com', 400, 600);">
my popup
</button>
<script>
function popupCenter(url, w, h) {
const y = window.top.outerHeight / 2 + window.top.screenY - ( h / 2);
const x = window.top.outerWidth / 2 + window.top.screenX - ( w / 2);
const newWindow = window.open(url, ' ',
`
scrollbars=yes,
width=${w},
height=${h},
top=${y},
left=${x}
`
)
if (window.focus) newWindow.focus();
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment