self-close.html (868B)
1 <!doctype html> 2 <p>self-close.html?navs=n&channel=name will navigate n times, then close and notify the channel.</p> 3 4 <script> 5 window.onload = () => setTimeout(() => { 6 const urlParams = new URLSearchParams(window.location.search); 7 let n = parseInt(urlParams.get('navs')) || 0; 8 9 const channel = new BroadcastChannel(urlParams.get('channel')); 10 11 channel.postMessage({ name: 'load', href: window.location.href }); 12 13 if (n > 0) { 14 urlParams.set('navs', n-1); 15 window.location.href = `${window.location.pathname}?${urlParams.toString()}#${n}`; 16 } else { 17 window.onbeforeunload = () => { 18 channel.postMessage({ name: 'beforeunload', history: history.length, closed: true }); 19 } 20 window.close(); 21 if (!window.closed) { 22 channel.postMessage({ name: 'close failed', history: history.length, closed: false }); 23 } 24 } 25 }, 0); 26 </script>