How to find out if a window can be closed using js

So, as said elsewhere, the window can be closed using js window.close()

only if the script was open.

I have a page that offers a button to open a discussion window. The discussion window opens to a new tab with window.open()

. The talk page has a trigger button window.close()

that closes the talk window and takes you back to the previous tab, so you can pick up where you left off.

The problem is if someone grabs the url directly from the talk window, the close button doesn't work.

Is there a way to determine if the window will close with window.close()

so I can only show the button if it works?

+3


source to share


2 answers


You can check if window.opener

not null

:



When a window is opened from another window, it maintains a link to that first window as window.opener. If there is no opener in the current window, this method returns NULL. Windows Phone browser does not support window.opener. It is also not supported in IE if the opener is in a different security zone.

+5


source


You can try using an object window.opener

that returns a link to the window that opens the current window (if it's another window), or NULL

if the current window was not opened via JS.



if (window.opener) //Show button

      

+2


source







All Articles