Self.close () does not work for mozilla

self.close () works fine in IE, but not in Mozilla. Does anyone know what the problem is and how can I solve it?

+3
source share
3 answers

Did you open the window with window.open? According to the docs on window.close :

This method is only allowed for windows opened with a script using the window.open method. If the window was not opened using a script, the following error appears in the JavaScript console: scripts may not close windows that were not opened using a script.

+9
source

Try using it instead window.close().

0
source

See my answer to this other question . You should be able to easily adapt it from ASP.NET to regular HTML.

Basically, since mozilla will let you close the window open by js, you can open a new window and set the target:

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

now your window was opened by js, and you can close it with js! :) close.html:

<html><head>
 <title></title>
 <script language="javascript" type="text/javascript">
     var redirectTimerId = 0;
     function closeWindow()
     {
         window.opener = top;
         redirectTimerId = window.setTimeout('redirect()', 2000);
         window.close(); 
     }

     function stopRedirect()
     {
         window.clearTimeout(redirectTimerId);
     }

     function redirect()
     {
         window.location = 'default.aspx';
     }
 </script>
 </head>
 <body onload="closeWindow()" onunload="stopRedirect()" style="">
     <center><h1>Please Wait...</h1></center>
 </body></html>
0
source

Source: https://habr.com/ru/post/1703394/


All Articles