I have the following scenario in my application: I currently have a service that creates a popup window below:
import { Injectable } from '@angular/core';
function _window() : any {
  return window;
}
@Injectable()
export class WindowRef {
  get nativeWindow(): any {
    return _window();
  }
}
I use this service to generate a popup according to the code below and it works perfectly:
gotoPopUp(scriptId, idleTime) {
    let url = '#/script-reader-details/'.concat(scriptId).concat('/').concat(this.agentId).concat('/').concat('25').concat('/').concat(idleTime);
    let width = 900;
    let height = 500;
    let left = (screen.width / 2) - ( width / 2 );
    let top = (screen.height / 2) - ( height / 2);
    this.winRef.nativeWindow.open(url, '_blank',
      'toolbar=no, location=no, status=no, menubar=no, scrollbars=no,resizable=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + '');
  }
I need to detect when the user clicks on the 'X' and closes that popup. I tried to use the Listeners below and follow the post in the link: How can we detect when user closes browser?
Sample:
  @HostListener('window:unload', [ '$event' ])
  unloadHandler(event) {
    this.updateIsRunningScript(this.agentId, false);
  }
  @HostListener('window:beforeunload', [ '$event' ])
  beforeUnloadHander(event) {
    this.updateIsRunningScript(this.agentId, false);
  }
but they are not invoked when clicking on the close button of the browser.
Is there any limitation between listener and nativeWindow?