Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if window.close() will work before using it (JavaScript)

According to many rules and security features, window.close() will only work in specific cases:

From the latest working spec for window.close():

The close() method on Window objects should, if all the following conditions are met, close the browsing context A:

  • The corresponding browsing context A is script-closable.
  • The browsing context of the incumbent script is familiar with the browsing context A.
  • The browsing context of the incumbent script is allowed to navigate the browsing context A.

A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a browsing context whose session history contains only one Document.

I have a web application that allows users to close new windows and it works fine, except when the rules above are not respected.

What I am looking for is to detect when the close() function will work and only show the close button in such case.

I found information talking about window.opener that returns a reference from the window that opened it. But it doesn't work.

if(window.opener != null){
//show button
}

Maybe this is because the new window was opened using "right click -> open in new tab" and not a script. When tabs are opened in this fashion window.close() works, I just want to detect when window.close() will work.

Any ideas?

like image 442
multimediaxp Avatar asked May 29 '18 20:05

multimediaxp


People also ask

How do you check if a window is closed in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

What does close () do in JavaScript?

close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.

How do I detect if a browser window is closed or refreshed?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How do I listen to my child closing windows?

If you store a reference to the child window when you call window. open() , then you can poll using setInterval() to see whether the window is still open using the window. closed property. The example below checks twice per second.


1 Answers

According to the docs, the window is script-closable also if session history of the given context is of length 1 (which is exactly what happens when you open a link in a new tab/window). You need to add that to your checker.

if(window.opener != null || window.history.length == 1){
//show button
}
like image 197
BoltKey Avatar answered Sep 20 '22 19:09

BoltKey