Created
November 7, 2022 14:17
-
-
Save fabio-filho/f3337412acaba0b2405688c1df0bdb78 to your computer and use it in GitHub Desktop.
Popup Window Centralized by Parent Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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