window.close does not work

Asked

Viewed 4,377 times

1

I am implementing the Jquery UI Dialog in my application. When the dialog is closed it should close the current window using the method window.open(); javascript.

However this does not occur, and the following error is still displayed on the console:

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

I’ve been searching for the solution on Google for some time, but everything I found was not enough. An example of massively accepted solution that did not solve my problem was the use of the following code (or similar variations):

window.open('', '_self', '');
window.close();

I simplified the code of my application to use example in this fiddle, although the error message is not displayed by Jsfiddle, nor is the window closed.

  • You have tried using jQuery.Dialog () from jQueryUI ??

  • That’s exactly who I’m using.

  • The method window.open returns the reference to the open window, where you can use the window.close on that same object. You can test that?

  • Down at the fiddle: parent.window.close(); shoots.

  • @Brasofilo yes, shoots yes.

  • @Wakim which window attribute triggers this reference?

  • The very one window.open returns the reference to the object window from the open window. Just save it and use later when you want to close.

  • @Wakim then I can pass it by parameter to the dialog function and use it there?

  • Yes, I believe you can pass, because of the call to dialog you will "save" it in a closure, I believe that with this the object will be available in the dialog action.

Show 4 more comments

3 answers

6

You can’t use it window.close in windows that have not been opened by the script in question, and that’s exactly what the message is saying.

Remember that you have the self.close() in the case of the same window:

<a href="self.close ()">Fechar esta janela</a>

But anyway, browsers offer some restrictions for these uses to prevent abuse, restricting to scripts to close windows opened by scripts, or with certain special conditions (depends on the implementation of each browser).

Simplifying:

  • The script from the "previous page", which created the new window is that you can close the window with window.close.

  • To use the self.close, on the other hand, normally the window has to have been opened by a script.

As the implementation varies a little from case to case, if anyone has any more important consideration, put in the comments that I increase the support

  • I used self.close; and it didn’t work either. That’s exactly what the warning message.

  • The script in question is called directly from the yes page, running the ajaxSubmit method from an onclick on a button.

  • But where is the close? In the script or open window? Try to edit the question and add all the details if possible.

  • In the script, inside the method called by the button.

  • and the script is in the window that opened the popup, or in the source of the popup window itself?

  • Not on my app or fiddle self.close(); closed the window.

  • 1

    but your fiddle didn’t open any window, as it will close?

Show 2 more comments

1


I figured out the reason for the problem that happens when the browser tab is reopened/resurrected, i.e., in Chrome when I reopen the window through the shortcut Ctrl + Shift + T.

Since I am testing this controller thoroughly I need to fill out a form with many fields, so I don’t have to fill them out again I reopen the tab from this shortcut, so when the command window.close() is executed the value of window is not known, because the tab is a orphan window (orphan window).

Thank you all for your help, but only after completely altering my way of testing have I found the cause of the problem.

0

To use jQuery dialogs do as follows:

$(seletor).dialog({
// opcoes que eu quero
});

To close the dialog you can use the button that is already available in the dialog or use the close command:

$(seletor).dialog("close");

One way I have done to avoid being selected is to save the selection in a global variable:

window.meuDialogo = $(seletor).dialog({
// Minhas opcoes
});

function fechaDialogo (){
  window.meuDialogo.dialog("close");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.