0

I want to open a window, which contains authorization form and handle result of authorization. I decided to do this by checking window location (if user was authorized - this will be updated) because didn't find a better way. But event handler not calling on every event. Can you please tell me why the events don't triggering or some alternative way to intercept the form result?

Might look like a duplication of this question, but in a window the event handlers are not overwritten.

How do I set event handlers:

import { Inject, Injectable } from '@angular/core';
// some other imports

@Injectable({
  providedIn: 'root'
})
export class WindowService {
  constructor(@Inject(WINDOW) private window: Window) {} // WINDOW is InjectionToken<Window>

  open(): void {
    let window = this.window.open('https://localhost:7202/Identity/Account/Login', '_blank');
    let eventHandler = function() {
      console.log(window!.location);
    };
    window!.onload = eventHandler;
    window!.addEventListener('load', eventHandler);
    window!.addEventListener('onload', eventHandler);
    window!.document.onload = eventHandler;
    window!.onhashchange = eventHandler;
    window!.addEventListener('hashchange', eventHandler, false);
  }
}
Anton Minchenko
  • 421
  • 5
  • 11
  • The window may be blocked by a popup blocker. You should check if the `window` variable exists with `if(window)` instead of forcing it using the exclamation mark. `window!.addEventListener()` might not work if `window` does not exist. – Kokodoko Mar 02 '22 at 10:12
  • @Kokodoko window exists, but I have removed this checking to simplify the code. – Anton Minchenko Mar 02 '22 at 10:20
  • 1
    It seems not possible to add a `load` listener to a window that is opened with `window.open`... – Kokodoko Mar 03 '22 at 11:30

0 Answers0