Created
June 29, 2020 11:01
-
-
Save subzey/e191ea4241746a91e4fd838cb10ba3c6 to your computer and use it in GitHub Desktop.
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
<!doctype html> | |
<html> | |
<head> | |
<title>New window open test</title> | |
</head> | |
<body> | |
<button id="openwnd">Open a new window</button> | |
<script> | |
function openTab(url) { | |
const a = document.createElement('a'); | |
a.href = url; | |
a.rel = 'noopener'; // decouple JS main loops | |
a.target = '_blank'; | |
a.style.display = 'none'; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
document.querySelector('#openwnd').addEventListener('click', (e) => { | |
e.preventDefault(); | |
fetch('/404/', { method: 'POST' }) | |
.catch(() => { | |
// We don't care how does server exactly respond | |
}) | |
.then(() => { | |
openTab('https://example.com/'); | |
}); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment