Skip to content

Instantly share code, notes, and snippets.

@junquera
Created February 12, 2018 15:56
Show Gist options
  • Select an option

  • Save junquera/347f9bce2e3249452a06b5cc4686d827 to your computer and use it in GitHub Desktop.

Select an option

Save junquera/347f9bce2e3249452a06b5cc4686d827 to your computer and use it in GitHub Desktop.
Manage father, son and brother windows
<script>
var sons = [];
function newSon(){
var next = window.open('/');
sons.push(next);
}
function closeSons(){
var next = sons.pop();
while(next){
try{
next.close();
} catch(e){}
next = sons.pop();
}
}
</script>
<body>
<h1 id="who">Parent</h1>
<button onclick="newSon()">Open son</button>
<a href="/" target="_blank"><button>Open "brother"</button></a>
<button onclick="closeSons()">Close sons</button>
<button id="suicide" onclick="window.close()" hidden>Close myself</button>
</body>
<script>
if(window.opener){
var element = document.getElementById('who');
element.innerHTML = "Son";
var suicide = document.getElementById('suicide');
suicide.removeAttribute('hidden');
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment