how I can close browser window with angular?

Viewed 646

I try many ways to close the browser window with angular 13 but don't work with me

I try this :

var win = window.open("about:blank", "_self");
win.close();
 window.opener = null;
    window.open("", "_self");
    window.close();
    window.location.href="javascript:window.close()"
1 Answers

You only have the power to close a tab if you were the one that opened that tab.

In the following example the close button will only work after opening a new tab using the link.

export class MyComponent {
  close() {
    window.close();
  }
}
<a href="http://localhost:4200" target="blank">Open New Tab</a><br />
<button (click)="close()">close</button>

From this article: https://www.thesitewizard.com/javascripts/close-browser-tab-or-window.shtml

On all modern browsers, the web page to be closed must be the first in that window/tab's session history.

And further:

Modern browsers will also resist your attempt to trick them into thinking that an existing window/tab was opened with JavaScript when it was not.

This is done to protect the user's session history.

Related