-2

I try to use this line to open up a new window and at the same time close the window from where the user clicked the link. It do open a new window but I get the following warning when trying to close the window. "Scripts may close only the windows that were opened by them"

<a href="#"><img src="images/close.png" onClick="CloseMe ('newwindow.html'),top.window.close();"></a>

<script language="JavaScript" type="text/javascript">
function CloseMe(url,h,w) {
 popupWin = window.open(url, 'popup', 'height=' + h + ',width=' + w + ',top=0,left=0');
}
</script>
MTplus
  • 1,377
  • 3
  • 21
  • 43

1 Answers1

1

The warning is right: Scripts are not allowed to close windows that were opened by the user and not a script.

So, you can close the window that you opened (and that window can also close itself), but you cannot close the other window that the user opened.

About your question how to close the window anyway: There is no way, it is not allowed! If you want to have a situation where you can close the previous window at any time, you need to use a script to open that window in the first place, then you can close it too. For example you could have your app initially just show a button "Enter application" which opens a new window using window.open with the actual application in it, and then calling window.close() will work inside of that window.

CherryDT
  • 20,425
  • 2
  • 39
  • 63