Only pages opened with window.open() can be closed with window.close()

In previous versions of Chrome, window.close() closed the current TAB directly, but for security reasons, only pages opened through window.open() could be closed through window.close(). Otherwise, the following error message is displayed:

Scripts may close only the windows that were opened by them.

Suppose I open window.open(b) on interface A and call window.close() on interface B to close interface B.

Now let’s explore how this works

open(url? : string, target? : string, features? : string, replace? : boolean): Window | null;

In the definition of the open method, we can see that an object of type Window is returned. Next, let’s look at what methods this object of type Window has.

opener: WindowProxy | null;

close(): void;

All we need to do is call its close() to close the current TAB. Close () destroys the object we originally created through open(). As for the opener, if you are not sure, you can set its value to NULL before close.

Welcome to comment on ^_^