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
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
Share Improve this question asked Dec 18, 2021 at 11:38 Tar WTar W 411 gold badge1 silver badge2 bronze badges 2- 1 Well, you can't... "scripts may close only the windows that were opened by them" by design in general. – Jeff Mercado Commented Dec 18, 2021 at 12:05
- Also, this has nothing to do with Angular – Jeremy Thille Commented Dec 18, 2021 at 12:49
1 Answer
Reset to default 5The 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 parison 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.