title: Angular 8 - capture window close
body: <p>I'm opening an external page in my web app using <code>window.open</code> and added a <code>addEventListner</code> for <code>unload</code> to capture window close. In the <code>unload</code> event block I want to perform some action and then redirect to another page.
  I'm able to open a new tab but as soon the tab is opened the listener code inside the <code>unload</code> block also get triggered. I want to trigger that code only after the window is closed.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;button type="button" (click)="goToUrl('https://mywebsite.com')"&gt;Click me!&lt;/button&gt;
  </code></pre>
  
  <p>Component:</p>
  
  <pre><code>verifyWindow: any;
  goToUrl(url: string): void {
      this.loading = true;
      this.verifyWindow = window.open(url, '_blank', 'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
  
      this.verifyWindow.addEventListener('unload', () =&gt; {
          this.userInfoService.accountInfo = {
              firstName: 'fname',
              lastName: 'lname',
              userName: 'uname',
              email: this.userInfo.email
          }
          this.router.navigateByUrl('register')
      });
  
  
  }
  </code></pre>