Unknown developer
Unknown developer

Reputation: 5920

Lifecycle hooks execute whereas they should not

In an Angular application, I tried to add

window.open("http:www.happyday.com", "_self");

inside the constructor of a component class.

I would expect at that point for the new URL to be loaded immediately. Instead, the app executes the code inside the constructor that comes after window.open(). Moreover, it executes lifecycle hooks like ngOnInit. Of course, it loads the above URL but after executing the whole lifecycle of the component (actually not ngOnDestroy). Could someone explain that behaviour?

Upvotes: 1

Views: 45

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73721

According to MDN documentation about Window.open(), the remote URL is loaded asynchronously:

Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

Upvotes: 2

Related Questions