0

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?

  • Seems to have worked for others https://stackoverflow.com/questions/37642589/how-can-we-detect-when-user-closes-browser/37642657#37642657 – Günter Zöchbauer Sep 01 '17 at 10:06
  • Using windows popup? – Fabio Mijas Sep 01 '17 at 10:08
  • I thought the problem is that `unloadHandler` and `beforeUnloadHandler` are not invoked. I doubt you can open some arbitrary popup. Browsers limit what is allowed in these event handlers. There should be enough answers about that though already, because this is not Angular related in any way. – Günter Zöchbauer Sep 01 '17 at 10:35

0 Answers0