Closing a Window in Internet Explorer (All Versions)

I found this unique solution that allows us to close a windows (the window not opened through Javascript) using the Javascript window.close() method in IE where IE throws a prompt like «The Webpage you are viewing is trying to close the window. Do you want to close this window?»

Due to the security enhancements in IE, we can not close a window unless it is opened by an script.

The way to solve this problem consists in letting the browser thinks that the page is opened using an script so we can close the window easily.

Below you can see the implementation of the code:

<script type=»text/javascript»>

          functioncloseWP() {
                   var Browser = navigator.appName;
                   var indexB = Browser.indexOf(‘Explorer’);
 
                   if (indexB > 0) {
                          var indexV = navigator.userAgent.indexOf(‘MSIE’) + 5;
                          var Version = navigator.userAgent.substring(indexV, indexV + 1);
                          if (Version >= 7) {
                                  window.open(», ‘_self’, »);
                                  window.close();
                         } else if (Version == 6) {
                                             window.opener = null;
                                             window.close();
                                  } else {
                                            window.opener = »;
                                            window.close();
                                 }
 
                   } else {
                     window.close();
                   }
           }

</script>

The original source of this code can be found in this link.

Deja una respuesta