window.close does not work

1

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

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

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

For some time I've been looking for the solution on Google, but everything I've found has not been enough. An example of a 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've simplified my application code to use example in this fiddle , although with JSFiddle the message for error, and neither window is closed.

    
asked by anonymous 18.09.2014 / 22:30

3 answers

1

I discovered the reason for the problem that happens when the browser flap is reopened / resurrected, ie in Chrome when I reopen the window through the Ctrl + Shift + T shortcut.

As I'm testing this controller I exhaustively need to fill out a form with many fields, so I do not have to fill them again I reopen the tab from this shortcut, so when the window.close() command is executed the window value is not known , because the tab is orphan window (orphan window).

Thanks to everyone for the help, but only after completely changing the way I tested that I found the cause of the problem.

    
19.09.2014 / 16:07
6

You can not use window.close on windows that were not opened by the script in question, and that's what the message is saying.

Remember that you have self.close() for the same window:

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

But in any case, browsers offer some restrictions on these uses to prevent abuse by restricting scripts to closing scripted windows, or with certain special conditions (depending on the implementation of each browser).

Simplifying a little:

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

As the implementation varies a bit from case to case, if anyone has any further important considerations, please put in the comments that I increased the response

    
18.09.2014 / 22:36
0

To use jQuery dialogs do the following:

$(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've done to avoid getting selected is to save the selection to a global variable:

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

function fechaDialogo (){
  window.meuDialogo.dialog("close");
}
    
18.09.2014 / 22:41