Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close browser tab (window) in angular

I there any trick that I can use to close browser tab (window) in angular. most of methods I tried are not working. the error I got is "scripts may close only the windows that were opened by them". help to ignore this

like image 983
Tar W Avatar asked Oct 28 '25 20:10

Tar W


1 Answers

The Window interface represents a window containing a DOM document; the document property points to the DOM document loaded in that window.

A global variable, window, representing the window in which the script is running, is exposed to JavaScript code.

Where the window.parent property returns the immediate parent of the current window, window.top returns the topmost window in the hierarchy of window objects.

You can simply use the following code:

window.top.close();

The Window self() property is used for returning the current window. It is generally used for comparison purposes while using other functions such as top().

You can simply use the following code:

window.self.close(); 

Warning!

Sometimes you can't close the current window in Firefox because you didn't open it. Most people have their browser set to open new windows in a new tab, and you can't prevent the menu bar etc in a tab window.

If you use close() method, This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console:

Scripts may not close windows that were not opened by script.

like image 153
Faramarz Avatar answered Oct 31 '25 11:10

Faramarz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!