Skip to content

Instantly share code, notes, and snippets.

@subzey
Created June 29, 2020 11:01
Show Gist options
  • Select an option

  • Save subzey/e191ea4241746a91e4fd838cb10ba3c6 to your computer and use it in GitHub Desktop.

Select an option

Save subzey/e191ea4241746a91e4fd838cb10ba3c6 to your computer and use it in GitHub Desktop.
<!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